2017-03-14 120 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}"