Contents of ComboBox determined by selected of previous

Nov 28, 2011 at 8:19 PM

I have two ComboBoxes.

The selection from the first ComboBox is used to filter the contents of the second ComboBox.

How do I get the second ComboBox to refresh its' list after the first ComboBox item has been selected?

 

 

Thanks,

Coordinator
Nov 29, 2011 at 4:44 AM

Ok, here's an example.

I'm going to use ListBoxes instead of ComboBoxes, just because it makes a nicer demo.  The key is that they share a DataContext (which they inherit from their parent), and the second box is bound to the "CurrentItem" (actually, in this case, it's bound to a PROPERTY of the CurrentItem).

As a data source for the example I'm using Get-ChildItem, and to give me a second list, I'm grouping the files (and folders) by extension:

StackPanel -Orientation Horizontal -DataContext { 
   Get-ChildItem | Sort-Object Extension | Group-Object Extension 
} -Children {
   ListBox -Width 75 -MinHeight 300 -DataBinding @{ ItemsSource = New-Binding -Path "." } -DisplayMemberPath Name -IsSynchronizedWithCurrentItem:$true
   ListBox -MinWidth 350 -DataBinding @{ ItemsSource = New-Binding -Path "CurrentItem.Group" }
} -Show

Nov 29, 2011 at 2:19 PM

Thanks Jaykul for the quick reply, it helps but because of code differences I'm not sure how to implement in my context, the main elements of my code are:

$servers = somestuff  #populated by invoke-sqlquery
 
new-grid -rows 3 -columns 6 -children{

  New-ComboBox -row 3 -column 1 -IsEditable:$true -IsTextSearchEnabled:$true -SelectedIndex 0 -Name serverName -items{$servers | select -expandproperty serverName} -on_selectionChanged {$dbs = invoke-sqlquery "EXEC sp_databases" -server $serverName.selectedItem} 
 
#how do I get object $dbs to refresh based on the selection above? 
new-ComboBox -row 4 -column 1 -IsEditable:$true -IsTextSearchEnabled:$true -SelectedIndex 0 -Name DBName -items {$dbs | select -expandproperty database_name} 
}

how do I get object $dbs to refresh based on the selection in combobox serverName?

any help appreciated.

Coordinator
Nov 29, 2011 at 5:47 PM
Edited Nov 29, 2011 at 5:54 PM

Since you're only going and getting the list in the SelectionChanged event handler, you want to do one of two things:

  1. Update the combobox explicitly in the event handler, or ...
  2. Stick the list of servers in the DataContext and add the list of databases to the server as a property (and then bind the second combobox to that property)

 

$servers = "DBServer1","DBServer2"
 
New-Grid -rows 3 {
	# Don't specify the SelectedIndex, that way the user has to select something AFTER the app starts up
	New-ComboBox -Width 200 -row 1 -column 1 -IsTextSearchEnabled:$true -Name serverName -items $servers -On_SelectionChanged {
		$DBName.ItemsSource = Invoke-SqlCmd "EXEC sp_databases" -server $this.SelectedValue
		$DBName.SelectedIndex = 1
	# If you want to start up with default values ... select them after everything is loaded:
	} -On_Loaded { 
		$this.SelectedIndex = 1
	}
	New-ComboBox -Width 200 -row 2 -column 1 -IsTextSearchEnabled:$true -Name DBName -DisplayMemberPath database_name
} -Show
Nov 29, 2011 at 8:38 PM

works a treat, thanks!  I owe you a guinness...