1

Everytime i change my selected item inside my UI it's not updating my combobox.

XAML

<ComboBox x:Name="CompanyComboBox" HorizontalAlignment="Left" Height="26" 
    Margin="100,41,0,0" VerticalAlignment="Top" Width="144" 
    SelectionChanged="CompanyComboBox_SelectionChanged" 
    SelectedItem="{Binding SelectedCompany, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ContentPresenter 
                Content="{Binding Converter={StaticResource DescriptionConverter}}">
            </ContentPresenter>
        </DataTemplate>
    </ComboBox.ItemTemplate>    
</ComboBox>

C#

private Company _selectedCompany;

public Company SelectedCompany
{
    get { return _selectedCompany; }
    set
    {
        if (value == _selectedCompany)
            return;
        _selectedCompany = value;
        OnPropertyChanged(nameof(SelectedCompany));
    }
}

Just to clarify the Company class is actually an ENUM

DescriptionConverter:

public class CompanyDescriptionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var type = typeof(Company);
        var name = Enum.GetName(type, value);
        FieldInfo fi = type.GetField(name);
        var descriptionAttrib = (DescriptionAttribute)
            Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));

        return descriptionAttrib.Description;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

what i mean by inside my UI, is i have a list of companies set to a combobox item source, and when i change the combobox value to another company, it doesn't update in my source, it stay's as default.

My Enum might clarify the problem for someone:

 [Description("Netpoint Solutions")]
    Company1 = 0,

    [Description("Blackhall Engineering")]
    Company2 = 180,
Corey James Carney
  • 167
  • 1
  • 2
  • 14
  • What does your DescriptionConverter look like? Is it ever invoked? And what exactly do you mean by "change my selected item inside my UI" -- does that mean you select something in this particular combobox? Seeing more of your code would be helpful. – Petter Hesselberg Sep 08 '17 at 11:45
  • @PetterHesselberg updated thread. – Corey James Carney Sep 08 '17 at 11:52

2 Answers2

2

Try to remove Mode=OneWayToSource and your event handler:

SelectionChanged="CompanyComboBox_SelectionChanged" 

You don't need to handle the SelectionChanged event when you bind the SelectedItem property.

Also make sure that you set the DataContext to an instance of your class where the SelectedCompany property is defined.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

Your Binding is defined as OneWayToSource. When you want it to be updated from the viewModel, you should set it to TwoWay.

See the documentation of Bindings for more details: https://msdn.microsoft.com/de-de/library/ms752347(v=vs.110).aspx

EDIT:

I skipped the part where you have an Enum as the source. I do not see, that you define the ItemsSource for the Combobox, where is this done? The value passed to the setter of SelectedCompany has to be in the Collection defined as ItemsSource.

For Enums, you can refer to this thread: How to bind an enum to a combobox control in WPF?

Eike B
  • 231
  • 1
  • 7