2017-01-25 17 views
0

手動で定義された列を持つDataGridを使用してWPFアプリケーションで作業しています。 そのDataGridでは、下限(10進値)と比較記号( "<"または "< =")を含むデータを入力できるようになります。WPFの既存のデータが表示されないObservableCollectionにバインドされたDataGridComboBoxColumn

データグリッド自体がStepDataSourceという名前のObservableCollectionにバインドされています

/// <summary> 
    /// Gets or sets the data of the stepfunction. 
    /// </summary> 
    public ObservableCollection<StepData> StepDataSource 
    { 
     get { return stepdataSource; } 
     set 
     { 
      stepdataSource = value; 
      RaisePropertyChanged("StepDataSource"); 
     } 
    } 

クラスStepData - のviewmodelの一部ではありません - 次のプロパティが含まれています

/// <summary> 
    /// Gets or sets the lower bound. 
    /// </summary> 
    public double LowerBound { get; set; } 

    /// <summary> 
    /// Gets or sets the assigned value. 
    /// </summary> 
    public double StepValue { get; set; } 

    /// <summary> 
    /// Gets or sets the lower comparer. 
    /// </summary> 
    public ArithmeticSignData LowerComparer2 { get; set; } 

最後のプロパティLowerComparer2 ComboBoxで選択されたアイテムに必要です。

DataGrid内の私のコラムの一つは、私のviewmodelに別のObservableCollectionにバインドされてDataGridComboBoxColumnです:

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

クラスArithmeticSignDataは、コンボボックスで使用しなければならないキーと値のプロパティが含まれています。私はこのフォーラムでは、いくつかの記事では

<DataGrid x:Name="grd_stepdata" 
       Grid.Row="0" 
       Grid.Column="0" 
       Margin="5" 
       AutoGenerateColumns="False" 
       CanUserAddRows="True" 
       CanUserDeleteRows="True" 
       SelectionUnit="FullRow" 
       ItemsSource="{Binding StepDataSource, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
     <DataGrid.Columns> 
      <DataGridComboBoxColumn x:Name="col_LowerComparer2" 
            SelectedItemBinding="{Binding LowerComparer2, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
            DisplayMemberPath="ArithmeticSignValue" 
            Header="LowComp"/> 
     </DataGrid.Columns> 
    </DataGrid> 

:私はそのようなデータグリッドで列を定義した後

 LowerComparers2 = new ObservableCollection<ArithmeticSignData>(); 
     LowerComparers2.Add(new ArithmeticSignData("1", "<")); 
     LowerComparers2.Add(new ArithmeticSignData("2", "<=")); 

:のviewmodelで

public class ArithmeticSignData 
{ 
    /// <summary> 
    /// The constructor. 
    /// </summary> 
    /// <param name="key">The key.</param> 
    /// <param name="value">The value.</param> 
    public ArithmeticSignData(string key, string value) 
    { 
     ArithmeticSignKey = key; 
     ArithmeticSignValue = value; 
    } 

    public string ArithmeticSignKey { get; set; } 

    public string ArithmeticSignValue { get; set; } 

} 

iは、そのコレクションを充填していますItemsSourceをコードビハインドで設定する必要があることを知りました。

col_LowerComparer2.ItemsSource = vm.LowerComparers2; 

私は、アプリケーションを起動し、アイテムを選択すると、私はviewmodelプロパティLowerComparer2で正しいキーと値で選択されたアイテムを見ることができます。 残念ながら、私はアプリを起動した後、列のビューモデルからの既存のデータを見ることができません。

1つ以上の属性を忘れましたか?

+0

がStepDataSourceコレクションがタイプのビューモデルすなわちオブジェクト、オブジェクトが含まれていますLowerComparer2とLowerComparers2プロパティが定義されていること:あなたはすでにLowerComparers2コレクションに追加されましたArithmeticSignDataオブジェクトのいずれかに設定する必要があり?あるいは、StepDataSourceとは何ですか?定義されている場所は? – mm8

答えて

2

これを試してみてください:

<DataGridComboBoxColumn x:Name="col_LowerComparer2" 
            SelectedItemBinding="{Binding LowerComparer2, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
            DisplayMemberPath="ArithmeticSignValue" 
            Header="LowComp"> 
    <DataGridComboBoxColumn.ElementStyle> 
     <Style TargetType="ComboBox"> 
      <Setter Property="ItemsSource" Value="{Binding Path=DataContext.LowerComparers2, RelativeSource={RelativeSource AncestorType=DataGrid}}" /> 
     </Style> 
    </DataGridComboBoxColumn.ElementStyle> 
    <DataGridComboBoxColumn.EditingElementStyle> 
     <Style TargetType="ComboBox"> 
      <Setter Property="ItemsSource" Value="{Binding Path=DataContext.LowerComparers2, RelativeSource={RelativeSource AncestorType=DataGrid}}" /> 
     </Style> 
    </DataGridComboBoxColumn.EditingElementStyle> 
</DataGridComboBoxColumn> 

はまたStepDataオブジェクトのLowerComparer2プロパティによって返されるオブジェクトを選択する項目についてMainViewModelClassのLowerComparers2コレクションに存在しなければならないことに注意してください。つまり、このプロパティをという新しいArithmeticSignDataオブジェクトに設定することはできません。

using System.Linq; 
... 

public MainWindow() 
{ 
    // The window components are initialized. 
    InitializeComponent(); 

    // DataContext gets the viewmodel. 
    DataContext = vm; 

    vm.StepDataSource.Add(new StepData("<", 0, 0, vm.LowerComparers2.FirstOrDefault(x => x.ArithmeticSignKey == "1"))); 
    vm.StepDataSource.Add(new StepData("<=", 0.1, 0.8, vm.LowerComparers2.FirstOrDefault(x => x.ArithmeticSignKey == "2"))); 
    vm.StepDataSource.Add(new StepData("<", 0.2, 1.2, vm.LowerComparers2.FirstOrDefault(x => x.ArithmeticSignKey == "1"))); 
    vm.StepDataSource.Add(new StepData("<=", 0.3, 1.4, vm.LowerComparers2.FirstOrDefault(x => x.ArithmeticSignKey == "2"))); 
    ChartDataRefresh(); 

    ... 
} 
+0

ありがとうございますmm8。最初の例でItemsSourceバインディングはXAMLでも動作します。しかし、私はまだ列col_LowerComparer2の既存のデータを見ることができません。私のソリューションのファイルをGitHubリポジトリにアップロードしました:https://github.com/patrickpirzer/StepFunctions –

+0

こんにちはmm8。ItemsSourceバインディングもXAMLでも動作しますが、アプリケーションを起動した後でもまだ "col_LowerComparer2"列の既存のデータを取得できません。一方私はGitHubリポジトリに自分のアプリケーションのファイルをインポートしました:https://github.com/patrickpirzer/StepFunctions –

+0

バインディングは機能しますが、データはまだ見えませんか?どういう意味ですか? – mm8