Reputation: 2975
I am using WPF. I have a LINQ query which populates ComboBox1
. The value selected from ComboBox1
should then be used in the where clause of a LINQ query which I want to populate ComboBox2
.
Is there an event trigger so that when a value has been selected in ComboBox1
, ComboBox2
will populate with LINQ query results? I've written the LINQ query, but not too sure how to make it run on the event.
This is what I want to run after ComboBox1
value has been selected
private void ComboBox2_Loaded(object sender, RoutedEventArgs e)
{
string comboName = ComboBox1.Text;
int recipId = RecipID(comboName);
ComboBox2.ItemsSource = MainWindow.qryGiftList(recipId);
ComboBox2.DisplayMemberPath = "cGift";
}
EDIT:
private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_comboSelection.cItem == "Gift")
{
string comboName = ComboBox1.Text;
int recipId = RecipID(comboName);
ComboBox2.ItemsSource = MainWindow.qryGiftList(recipId);
ComboBox2.DisplayMemberPath = "cGift";
}
}
I also tried instead of using string ComboName = ComboBox1.Text;
to use string ComboName = (string)ComboBox1.SelectedItem
but this results in a debug of:
An unhandled exception of type 'System.InvalidCastException' occurred in myMemory.exe Additional Information: Unable to cast object of type 'myMemory.Name' to type 'System.string'.
Upvotes: 0
Views: 101
Reputation: 612
The right event is SelectionChanged.
<ComboBox x:Name="combobox1" SelectionChanged="comobox1_SelectionChanged"/>
You can get the selected value in the event like this
string value = (string)combobox1.SelectedItem
;
Upvotes: 1