Alaor
Alaor

Reputation: 2191

WPF ComboBox doesn't change the selected item

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

Upvotes: 2

Views: 5722

Answers (2)

Ying
Ying

Reputation: 381

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.

Upvotes: 0

lazo
lazo

Reputation: 583

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}">

Upvotes: 5

Related Questions