2

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!!

2 Answers 2

5

I hope I understood your question correctly. Is your DataContext a Category object? Sounds to me like you need to bind the SelectedItem property of the ComboBox. E.g.:

<ComboBox Grid.Column="1" x:Name="motherCategoryComboBox" Margin="0,6,12,1" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding MotherCategory , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
Sign up to request clarification or add additional context in comments.

4 Comments

I don't set the DataContext, I pass a List<Category> direct to the ItemSource property of the combobox.
Sorry man, I accepted your answer, but I found that the origin of the behavior was more obscure. I've overriden the Equals method of Category and then it was causing strange things to happen in the combobox.
And ps: I want to kill myself.
Oops. Haha.. hope you're live n kickin' & managed to solve this by now!
0

It's not your case but since it happened to me I'm publishing this here, to help other people who might stumble upon this issue...

During the comboBox SelectionChangeCommitted() event handler I added the following line:

combobox.Text = combobox.Text.Trim(); 

what it did is reset the selectedIndex and selectedText properties and didn't allow them to change to the new value due to keyboard or mouse input.

1 Comment

If you do this, wouldn't it affect the displayed result in the dropdown anytime you actually select it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.