Associate a combo box with WPF combo box (bi-directional)

I had a problem linking the selected value of the combo box embedded in the list view. I have no problem displaying items in a combo box. However, I wish I had a way to determine that the combo box should be displayed (from among the elements it contains) with the click of a button. I think there are a few posts on this subject, however I cannot get exactly what I want. Here is my code.

XAML:

<Grid> <StackPanel Orientation="Vertical"> <ListView x:Name="OptionsListView" ItemsSource="{Binding MyObjectCollection}"> <ListView.Resources> <DataTemplate x:Key="comboBoxTemplate"> <ComboBox Margin="0,3" x:Name="MyTypeComboBox" SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}"> <ComboBoxItem Content="ABC"/> <ComboBoxItem Content="DEF"/> <ComboBoxItem Content="XYZ"/> </ComboBox> </DataTemplate> </ListView.Resources> <ListView.View> <GridView> <GridViewColumn Header="Text-Sample" Width="100"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Name}"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="Combo-Sample" Width="100" CellTemplate="{StaticResource comboBoxTemplate}" /> </GridView> </ListView.View> </ListView> <Button Click="Button_Click">Click Me!</Button> </StackPanel> </Grid> 

C # code:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); OptionsListView.DataContext = this; } private void Button_Click(object sender, RoutedEventArgs e) { //Something here that dictates what should be displayed in the combo box } List<MyObject> myObjectCollection = new List<MyObject>(); public List<MyObject> MyObjectCollection { get { myObjectCollection.Add(new MyObject("One")); myObjectCollection.Add(new MyObject("Two")); return myObjectCollection; } } } public class MyObject : INotifyPropertyChanged { private string _name; public MyObject(string name) { // TODO: Complete member initialization this._name = name; } public string Name { get { return _name; } } string selectedType = string.Empty; public string SelectedType { get { return selectedType; } set { selectedType = value; this.NotifyChange("SelectedType"); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void NotifyChange(params object[] properties) { if (PropertyChanged != null) { foreach (string p in properties) { PropertyChanged(this, new PropertyChangedEventArgs(p)); } } } #endregion } 

I would be glad if someone helped me hack this.

Thanks Ram

+4
source share
2 answers

I am not sure if I did not understand your question. I think your problem is related to the reference problem. I changed the code a bit and it works when I click the button.

See code below.

XAML:

 <ComboBox Margin="0,3" x:Name="MyTypeComboBox" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=DataContext.Sources}" SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}"> </ComboBox> 

C # code:

 private Collection<string> sources = new Collection<string>() { "ABC", "DEF", "XYZ" }; public Collection<string> Sources { get { return sources; } } private void Button_Click(object sender, RoutedEventArgs e) { myObjectCollection[0].SelectedType = Sources[0]; myObjectCollection[1].SelectedType = Sources[2]; } 
+5
source

What about

 foreach (ComboBox c in OptionsListView.Items) { c.SelectedValue = "Put your value here"; } 

This should do the job, if you have other objects besides the dropdowns inside, you can add

 if (c is ComboBox) 

to make sure you are working on the correct object

0
source

All Articles