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