Answered by:
Data binding to ComboBoxes

Question
-
My problem here is, that I don't know how to bind SelectedIndex of ComboBox. I already achieved ItemSource binding. Works great. But now I wish to bind SelectedIndex to another object using some kind of converter. Actually I am working on filtering trough results(sending request to server...).
Here is xaml example of one specific ComboBox
<ComboBox Grid.Row="1" x:Name="showStatus_picker" Style="{StaticResource basicDropDown}" Width="400" ItemsSource="{Binding}" > <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Value}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
C# code
public class filterDropValues
{
public filterDropValues(string key, string value)
{
this.Key = key;
this.Value = value;
}
public string Key { get; private set; }
public string Value { get; private set; }
}dropDownList[0].Add(new filterDropValues("ALL", loader.GetString("filterALL")));
dropDownList[0].Add(new filterDropValues("Mine", loader.GetString("filterMine")));
dropDownList[0].Add(new filterDropValues("val1", loader.GetString("val1")));
dropDownList[0].Add(new filterDropValues("val2", loader.GetString("val2")));
dropDownList[0].Add(new filterDropValues("val3", loader.GetString("val3")));showStatus_picker.DataContext = dropDownList[0];
All that code works just fine. But as I mentioned before, I need binding for SelectedIndex. Two way direction. So when I change SelectedIndex, object value will also changed to selected value(trough converter I suppose).
I don't wish total solution, only push me to the right direction. I will write converter and everything on my own, I only need an idea, how to bind SelectedIndex in xaml.
Actually I decide to ask you guys here, because I really couldn't find the solution for past few days. I see only one solution here, but it is really ugly...when user interact with any control on my filter I will have to manually check if value is changed for each and every control...
Sorry for long post, I hope, it was understandable and sorry for my poor English. Thanks
- Edited by Klemzy2013 Friday, December 13, 2013 10:09 PM
Friday, December 13, 2013 10:06 PM
Answers
-
Hi,
You can register a dependency property in a class. Set the combobox DataContext to the calss object. Then bind the combobox selectvalue to the dependency property using a converter. There is a sample you can refer to:
XAML:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.Resources> <local:ToStringConverter x:Key="Converter1"/> </Grid.Resources> <ComboBox Margin="324,233,403,363" x:Name="showStatus_picker" ItemsSource="{Binding dropDownList }" SelectedValue="{Binding Index, Converter={StaticResource Converter1},Mode=TwoWay}" > </ComboBox> <Button Content="Button" HorizontalAlignment="Left" Margin="1172,622,0,0" VerticalAlignment="Top" Click="Button_Click"/> </Grid>
Code-behind:
public sealed partial class MainPage : Page { public static TestViewModel testviewmodel = new TestViewModel(); public MainPage() { this.InitializeComponent(); showStatus_picker.DataContext = testviewmodel; } private void Button_Click(object sender, RoutedEventArgs e) { testviewmodel.Index = 1; } } public class TestViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private ObservableCollection<string> _dropDownList = new ObservableCollection<string> { "filterALL", "filterMine", "val1", "val2" }; public ObservableCollection<string> dropDownList { get { return _dropDownList; } } private int index; public int Index { get { return this.index; } set { if (this.index != value) { this.index = value; if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("Index")); } } } } public class ToStringConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, string language) { string selectvalue; int index =(int)value; selectvalue = MainPage.testviewmodel.dropDownList[index]; return selectvalue; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } #endregion }
When click the button the Index would change so that the Combobox SelectValue also would change.
Best Wishes!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Edited by Anne Jing Monday, December 16, 2013 8:15 AM edit
- Marked as answer by Klemzy2013 Monday, December 16, 2013 8:39 AM
Monday, December 16, 2013 8:14 AM