views:

63

answers:

1

Hi people,

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

+1  A: 

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}">
lazo
I don't set the DataContext, I pass a List<Category> direct to the ItemSource property of the combobox.
Alaor
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.
Alaor
And ps: I want to kill myself.
Alaor
lazo