Вопрос

I am using this: Databinding an enum property to a ComboBox in WPF databinding for my comboboxes. I am unable to set the value of the comboboxes programatically though. Once it is bound, I can't set the SelectedItem, SelectedValue, or Text.

There must be a way to do this? Any help is appreciated.

To clarify, I have a comboBox bound to an enum that has all 50 states. I have a state value of the same type as the enum that the comboBox is bound to. I want to set the comboBox value to the value of my state.

Это было полезно?

Решение

If you bind the SelectedItem of the ComboBox to an underlying class, you should be able to change the binding by changing that class.

For example, let's say your enum was called "Country" and you had a class called "Person" and that person had a property called "CountryOfOrigin" and you want to bind it to a ComboBox. You could do this:

XAML file:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestingWPF"
    x:Class="TestingWPF.TestWindow">

    <Window.Resources>
        <ObjectDataProvider MethodName="GetValues"
        ObjectType="{x:Type local:Country}"
        x:Key="Countries">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:Country" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>

    <StackPanel>
        <ComboBox x:Name="comboBox"
              HorizontalAlignment="Center"
              VerticalAlignment="Center"
              Width="100" Margin="10"
              ItemsSource="{Binding Source={StaticResource Countries}}"
              SelectedItem="{Binding Path=CountryOfOrigin, Mode=TwoWay}"/>
        <Button HorizontalAlignment="Center" Content="Change Country to Mexico" Margin="10" Click="Button_Click"/>
    </StackPanel>
</Window>

Code-behind:

public partial class TestWindow : Window
{
    Person p;

    public TestWindow()
    {
        InitializeComponent();

        p = new Person();
        p.CountryOfOrigin = Country.Canada;

        DataContext = p;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        p.CountryOfOrigin = Country.Mexico;
    }
}

public enum Country
{
    Canada,
    UnitedStates,
    Mexico,
    Brazil,
}

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Country _countryOfOrigin;

    public Country CountryOfOrigin
    {
        get
        {
            return _countryOfOrigin;
        }
        set
        {
            if (_countryOfOrigin != value)
            {
                _countryOfOrigin = value;

                PropertyChanged(this, new PropertyChangedEventArgs("CountryOfOrigin"));
            }
        }
    }
}

Другие советы

I found this article very useful while working with enumerations binding to ComboBoxes, there are also examples of how to organize a conversion for displaying enum using its attribute values.
So a user can see f.e. ">" sign instead of enum named Greater

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top