微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。