WPF - 使用 TwoWay 模式将枚举绑定到 DatagridComboBoxColumn

问题描述

我正在尝试创建一个 DataGrid 表,其中 DatagridComboBoxColumn 列具有枚举中的可用选项。下面的当前代码只能从绑定 OneWay 绑定枚举值 PaletteColors。我希望当用户从下拉列表中选择枚举值时更新 PaletteColors[x].Type 中的枚举值。我们如何更新 xaml 来做到这一点?

Table.xaml

    <Grid.Resources>
        <!-- 
        Resource for displaying enum values
        -->
        <ObjectDataProvider x:Key="PaletteColorInterfaceTypes"
                            MethodName="GetValues" 
                            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="enums:PaletteColorInterfaceType" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Grid.Resources>

    <DataGrid
              IsReadOnly="False"
              ItemsSource="{Binding Path=PaletteColors}"
              SelectedItem="{Binding SelectedColor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="Location" ItemsSource="{Binding Source={StaticResource PaletteColorInterfaceTypes},Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" SelectedValueBinding="{Binding Path=Type}" Width="*" />
        </DataGrid.Columns>
        </DataGrid>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftClick">
                    <i:InvokeCommandAction Command="{Binding ColorSelectedCommand}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="CellEditEnding">
                    <command:EventToCommand PassEventArgsToCommand="True" Command="{Binding CellEditedCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

枚举

public enum PaletteColorInterfaceType
{
    Invalid = 0,A,B,}

PaletteColor.cs

public class PaletteColor : IEquatable<PaletteColor>
{
    public PaletteColor(
        string name,PaletteColorInterfaceType type)
    {
        this.Name = name;
        this.Type = type;
    }

    public string Name { get; set; }

    public PaletteColorInterfaceType Type { get; set; }
}

Tableviewmodel.cs

public SortableObservableCollection<PaletteColor> PaletteColors { get; }

public PaletteColor SelectedColor { get; set; }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)