I'm a newbie in WPF, so it probably is something very basic that I'm forgetting to do but I can't see what it is.
I have a window with a combobox that display some data, I want the user to select a category in this combobox. It's working partially. The window show the combobox, starting with no selection, then the user choose a item, and it's set, but if the user try to change to other item, nothing works, it keeps the original selected item.
Here's me code:
[Category class]
public class Category { public long CategoryId { get; set; } public string Name { get; set; } public Category MotherCategory { get; set; } public ICollection<Category> Categories { get; set; } public int Align { get; set; } } [ComboBox XAML]
<ComboBox Grid.Column="1" x:Name="motherCategoryComboBox" Margin="0,6,12,1" IsSynchronizedWithCurrentItem="True"> <ComboBox.Resources> <converter:LeftMarginConverter x:Key="LeftMarginConverter" /> </ComboBox.Resources> <ComboBox.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Path=Categories}"> <TextBlock Text="{Binding Path=Name}" Margin="{Binding Path=Align, Converter={StaticResource LeftMarginConverter}}" /> </HierarchicalDataTemplate> </ComboBox.ItemTemplate> </ComboBox> [Window code-behind file]
public CategoryWindow() { InitializeComponent(); db = new JaspeContext(); categorieslist = db.Categories.ToList(); motherCategoryComboBox.ItemsSource = categorieslist; Title = "Add category"; } [The converter]
public class LeftMarginConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { double leftMargin = double.Parse(value.ToString()); if (leftMargin != 1) leftMargin = leftMargin * 9; return new Thickness(leftMargin, 0, 0, 0); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new Exception("The method or operation is not implemented."); } } Need your help. This is making me crazy!
Thanks!!