2017-03-14 77 views
1

我正在使用MVVM与WPF应用程序上的VS 2015合作,并希望将DataGridComboBoxColumn绑定到viewmodel。 组合框应包含“<”(带键“1”)和“< =”(带键“2”)。如何绑定WPF DataGrid的DataGridComboBoxColumn

首先我必须与comboboxitems一个ObservableCollection:

public ObservableCollection<ArithmeticSignData> LowerComparerItems { get; set; } 

这是类ArithmeticSignData:

public class ArithmeticSignData 
{ 
    public ArithmeticSignData(string key, string value) 
    { 
     ArithmeticSignKey = key; 
     ArithmeticSignValue = value; 
    } 

    public string ArithmeticSignKey { get; set; } 
    public string ArithmeticSignValue { get; set; } 
} 

当我的视图模型初始化我填充列表LowerComparerItems:

private void FillLowerComparerItemsList() 
{ 
    LowerComparerItems = new ObservableCollection<ArithmeticSignData>(); 
    LowerComparerItems.Add(new ArithmeticSignData("1", "<")); 
    LowerComparerItems.Add(new ArithmeticSignData("2", "<=")); 
} 

datagrid的数据来自另一个observablecol用实体框架表作为类型。 该表有一个名为“low_operator”的列。 所以我认为它可以通过以下方式绑定comboboxcolumn。 当我打开组合框时,我可以看到项目。 但启动应用程序后,表中的值不会被翻译为“<”或“< =”。

<DataGridComboBoxColumn x:Name="cbc_LowerComparer" 
            SelectedItemBinding="{Binding low_operator, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
            DisplayMemberPath="ArithmeticSignValue" 
            Header=" " 
            Width="30"> 
    <DataGridComboBoxColumn.ElementStyle> 
     <Style TargetType="ComboBox"> 
      <Setter Property="ItemsSource" Value="{Binding Path=DataContext.LowerComparerItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" /> 
     </Style> 
    </DataGridComboBoxColumn.ElementStyle> 
    <DataGridComboBoxColumn.EditingElementStyle> 
     <Style TargetType="ComboBox"> 
      <Setter Property="ItemsSource" Value="{Binding Path=DataContext.LowerComparerItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" /> 
      <Setter Property="IsEditable" Value="True"/> 
     </Style> 
    </DataGridComboBoxColumn.EditingElementStyle> 
</DataGridComboBoxColumn> 

回答

1

您应该Selected价值Binding属性为您的结合和设置SelectedValuePath属性为“ArithmeticSignKey”:

<DataGridComboBoxColumn x:Name="cbc_LowerComparer" 
            SelectedValueBinding="{Binding low_operator, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
            SelectedValuePath="ArithmeticSignKey" 
            DisplayMemberPath="ArithmeticSignValue" 
            Header=" " 
            Width="30"> 

这应该将low_operator列设置为所选ArithmeticSignKey值的值。如果您想将其设置为ArithmeticSignValue,则应该将列的SelectedValuePath属性设置为该列的名称。

+1

非常感谢mm8! ComboBoxes非常困难,但在DataGrid中,它们要困难得多。 –

+0

不客气,但请记住投票赞成有用的答案:) – mm8

1

编辑您的代码是这样的:

<DataGridComboBoxColumn x:Name="cbc_LowerComparer" 
           ItemsSource="{Binding LowerComparerItems, UpdateSourceTrigger=PropertyChanged}" 
           SelectedItemBinding="{Binding low_operator, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
           DisplayMemberPath="ArithmeticSignValue" 
           Header=" " 
           Width="30"> 
1

而是SelectedItemBinding的使用...

SelectedValueBinding="{Binding low_operator}"