【问题标题】:WPF DataGridComboboxColumn not getting property (value from Database) binding to enumWPF DataGridComboboxColumn 未将属性(来自数据库的值)绑定到枚举
【发布时间】:2014-04-24 17:41:11
【问题描述】:

我在获取要在 DataGridComboBoxColumn 中显示的属性时遇到问题。但是,当从此组合框中选择一个值时,setter 将被更新,并且数据库将在保存时更改。 (所以基本上它的工作 UI -> 模型,但我不认为模型 -> UI ..)

基本上我有一个绑定到枚举的 DataGridComboBoxColumn。这是我认为问题所在的 XAML。

<DataGrid x:Name="dgProductItem" 
          ItemsSource="{Binding ProductVersion.ProductItems}"

<DataGridComboBoxColumn Header="Deployment Type" 
        SelectedItemBinding="{Binding DeploymentType, Mode=TwoWay}"
        SelectedValuePath="DeploymentType" Width="120">

这是 DataGridComboBoxColumn 的其余代码

<DataGridComboBoxColumn.ElementStyle>
    <Style TargetType="ComboBox">
        <Setter Property="ItemsSource"
                Value="{Binding Source={StaticResource DeploymentTypeEnum}}"/>
        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
    </Style>
</DataGridComboBoxColumn.ElementStyle>


<DataGridComboBoxColumn.EditingElementStyle>
    <Style TargetType="ComboBox">
        <Setter Property="ItemsSource"
                Value="{Binding Source={StaticResource DeploymentTypeEnum}}"/>
    </Style>
</DataGridComboBoxColumn.EditingElementStyle>

但正如我所说,当用户从组合框中选择一个值时,setter 将被更新,所以我认为它不会太远。

这是物业代码。

public DeploymentType DeploymentType
{
    get
    {
        return m_DeploymentType;
    }

    set
    {
        m_DeploymentType = value
        PropertyChanged("DeploymentType")
    }
}

任何帮助都会很有用。

谢谢

【问题讨论】:

标签: c# wpf xaml datagrid enums


【解决方案1】:

要获取列表,您应该有办法从枚举类型中提取

XAML

在资源级别设置:

    <ObjectDataProvider x:Key="DeploymentTypeEnum" MethodName="GetValues"
                        ObjectType="{x:Type System:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:DeploymentType"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

不要忘记在 xaml 中添加正确的命名空间

         xmlns:local="clr-namespace:ProjectNamespace"
         xmlns:System="clr-namespace:System;assembly=mscorlib"

【讨论】:

  • 嗨 Lucas,我​​已经有了这段代码(我可以将枚举类型绑定到组合框),但我只需要将数据库中的类型绑定到组合框。但我认为它目前只是选择默认或第一个枚举
  • 你是说你在DeploymentType中设置的值没有进入DataGrid?
  • 嗨卢卡斯,这正是发生的事情,但我现在得到了它的工作,所以会发布答案。
猜你喜欢
  • 2018-06-09
  • 2011-12-21
  • 1970-01-01
  • 2022-06-10
  • 2010-09-08
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多