Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式设置枚举绑定组合框的值_C#_Wpf_Combobox_Enums_Databound - Fatal编程技术网

C# 以编程方式设置枚举绑定组合框的值

C# 以编程方式设置枚举绑定组合框的值,c#,wpf,combobox,enums,databound,C#,Wpf,Combobox,Enums,Databound,我使用这个:组合框的数据绑定。不过,我无法通过编程设置组合框的值。绑定后,我无法设置SelectedItem、SelectedValue或文本 一定有办法做到这一点?感谢您的帮助 为了澄清,我将一个组合框绑定到一个包含所有50个状态的枚举。我有一个与comboBox绑定到的枚举类型相同的状态值。我想将comboBox值设置为我的状态值 如果将组合框的SelectedItem绑定到基础类,则应该能够通过更改该类来更改绑定 例如,假设您的枚举名为“Country”,您有一个名为“Person”的类,

我使用这个:组合框的数据绑定。不过,我无法通过编程设置组合框的值。绑定后,我无法设置SelectedItem、SelectedValue或文本

一定有办法做到这一点?感谢您的帮助


为了澄清,我将一个组合框绑定到一个包含所有50个状态的枚举。我有一个与comboBox绑定到的枚举类型相同的状态值。我想将comboBox值设置为我的状态值

如果将组合框的SelectedItem绑定到基础类,则应该能够通过更改该类来更改绑定

例如,假设您的枚举名为“Country”,您有一个名为“Person”的类,此人有一个名为“CountryOfOrigin”的属性,您希望将其绑定到一个组合框。您可以这样做:

XAML文件:

<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>
我发现在处理绑定到组合框的枚举时非常有用,还有一些示例说明如何组织转换以使用枚举的属性值显示枚举。

因此,用户可以看到f.e.“>”符号而不是名为更大的枚举

只是为了澄清,您使用的是投票率最高的答案还是问题中公布的答案?投票率最高的答案,即扩展枚举数类型的答案。这将起作用cmbenum.SelectedItem=ExampleEnum.Enum4;我用这种方法制作了一个小样本,对我来说效果很好。我可以从viewmodel设置值。很难说你到底有什么问题,因为我没有做什么特别的事。你可以在这里试用我的示例应用程序,看看它是否有帮助:你的示例非常有效。由于某些原因,我无法在代码中使PropertyChangedEventHandler不为null。
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"));
            }
        }
    }
}