简体   繁体   English

WPF C# - 将枚举绑定到 ComboBox

[英]WPF C# - Binding Enum to ComboBox

I have a combobox with the following options: "HardDelete", "SoftDelete", "MoveToDeletedItems"我有一个 combobox 具有以下选项:“HardDelete”、“SoftDelete”、“MoveToDeletedItems”

I want the default selection to match with the following property of an EmailAction object:我希望默认选择与 EmailAction object 的以下属性相匹配:

public DeleteMode DeleteMode { get; set; }

Here is the line of code I'm using to try to set this:这是我用来尝试设置的代码行:

cmboDelMode.SelectedItem = emailActionInstance.DeleteMode.ToString();

Related XAML:相关 XAML:

<ComboBox x:Name="cmboDelMode" HorizontalAlignment="Left" Margin="149,218,0,0" VerticalAlignment="Top" Width="120">
    <ComboBoxItem Content="HardDelete" HorizontalAlignment="Left" Width="118"/>
    <ComboBoxItem Content="SoftDelete" HorizontalAlignment="Left" Width="118"/>
    <ComboBoxItem Content="MoveToDeletedItems" HorizontalAlignment="Left" Width="118"/>
</ComboBox>

Currently the combobox is defaulting to being empty so is not working as expected.目前 combobox 默认为空,因此无法按预期工作。 I am able to use "emailActionInstance.DeleteMode.ToString();"我可以使用“emailActionInstance.DeleteMode.ToString();” to view the data in a text box, so it seems I might just be setting the selected item incorrectly?在文本框中查看数据,所以看来我可能只是错误地设置了所选项目?

The issue is that setting ComboBox.SelectedItem doesn't work unless the ComboBox contains the item you are setting it to.问题是设置 ComboBox.SelectedItem 不起作用,除非 ComboBox 包含您要设置的项目。 In your case your ComboBox is filled with three ComboBoxItem objects which have their Content property set to a string.在您的情况下,您的 ComboBox 填充了三个 ComboBoxItem 对象,它们的 Content 属性设置为字符串。 So ComboBox.SelectedItem is a ComboBoxItem.所以 ComboBox.SelectedItem 是一个 ComboBoxItem。 You are trying to set the ComboBox.SelectedItem to a string, which will not equal any of the objects contained in the ComboBox.您正在尝试将 ComboBox.SelectedItem 设置为字符串,该字符串不等于 ComboBox 中包含的任何对象。 Therefore, nothing happens.因此,什么也没有发生。

I would suggest setting up Binding for your ComboBox like here:我建议为您的 ComboBox 设置绑定,如下所示:

<ComboBox ItemsSource="{Binding DeleteModes}" SelectedItem="{Binding SelectedDeleteMode}"  />

And then create a ViewModel to bind to.然后创建一个要绑定的 ViewModel。 If you bind an Enum to a ComboBox it will display correctly so you won't need to call DeleteMode.ToString():如果将 Enum 绑定到 ComboBox,它将正确显示,因此您无需调用 DeleteMode.ToString():

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        DeleteModes = new ObservableCollection<DeleteMode>
        { DeleteMode.HardDelete, DeleteMode.SoftDelete,
          DeleteMode.MoveToDeletedItems };
    }

    public event PropertyChangedEventHandler PropertyChanged;

    DeleteMode _selected_delete_mode;
    public DeleteMode SelectedDeleteMode {
        get { return _selected_delete_mode; }
        set {
            _selected_delete_mode = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedDeleteMode"));
        }
    }

    ObservableCollection<DeleteMode> _delete_modes;
    public ObservableCollection<DeleteMode> DeleteModes {
        get { return _delete_modes; }
        set {
            _delete_modes = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DeleteModes"));
        }
    }

    public void update_mode(DeleteMode mode) => SelectedDeleteMode = mode;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM