1

すべて、次のように定義されたいくつかのテストデータで動作するいくつかのテストコードがあります

private ObservableCollection<TestClass> _testData = new ObservableCollection<TestClass>();
public ObservableCollection<TestClass> TestData
{
    get { return _testData; }
    set { _testData = value; }
}

設計時にバインドされます

<DataGrid x:Name="dataGrid" ItemsSource="{Binding TestData}".../>

DataGridを介して実行時に入力される を作成しました

dataGrid.ItemsSource = BuildDataGridColumns(cultureDict).Tables[0].AsDataView();

private DataSet BuildDataGridColumns(Dictionary<string, string> cultureDict,
                                     DataTable additionalDt = null)
{
    // ...
}

これはうまく機能しますが、テストデータで動作していたグリッドのビジュアルを更新するコードは、(私が信じている) 存在しないため、もはや機能しませんItemsSource="{Binding TestData}"。に挿入されたテキストTextBoxと のセルにあるテキストをバインドすることになっている XAML は次のDataGridとおりです。

<DataGrid x:Name="dataGrid" 
          local:DataGridTextSearch.SearchValue="{Binding ElementName=searchBox, Path=Text, UpdateSourceTrigger=PropertyChanged}" 
          AlternatingRowBackground="Gainsboro" AlternationCount="2" HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch">
   <DataGrid.Resources>
      <local:SearchValueConverter x:Key="searchValueConverter" />
      <Style TargetType="{x:Type DataGridCell}">
         <Setter Property="local:DataGridTextSearch.IsTextMatch">
            <Setter.Value>
               <MultiBinding Converter="{StaticResource searchValueConverter}">
                  <Binding RelativeSource="{RelativeSource Self}" Path="Content.Text" />
                  <Binding RelativeSource="{RelativeSource Self}" Path="(local:DataGridTextSearch.SearchValue)" />
               </MultiBinding>
            </Setter.Value>
         </Setter>
         <Style.Triggers>
            <Trigger Property="local:DataGridTextSearch.IsTextMatch" Value="True">
               <Setter Property="Background" Value="Orange" />
            </Trigger>
         </Style.Triggers>
      </Style>
   </DataGrid.Resources>
   <DataGrid.CellStyle>
      <Style TargetType="DataGridCell" >
         <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
               <Setter Property="Background" Value="#FF007ACC"/>
               <Setter Property="Foreground" Value="White"/>
            </Trigger>
         </Style.Triggers>
      </Style>
   </DataGrid.CellStyle>
</DataGrid>

私の質問は;

DataGrid実行時に作成するデータ セットにmy と同じバインディングを作成するにはどうすればよいですか?

私のデータがインターフェイスを実装していないことが理由だと誰かが示唆していますが、データの可変性のために、実行時INotifyCollectionChangedにデータをロードする必要があります。DataGrid

DataGridデータの構造が変更される可能性がある場合、このデータを実装するクラスとしてバインドするにはどうINotifyPropertyChangedすればよいですか?

ありがとうございました。

4

1 に答える 1

1

私はブラムが正しいと思います。(申し訳ありませんが、まだコメントを追加する権限がありません。) テスト コードでは、INotifyPropertyChanged を実装する ObservableCollection を使用します。DataGrid は、そのコレクションへの変更を取得します。実際のコードで ObservableCollection を使用し、dataGrid.ItemsSource を介して直接ではなく、バインドされた DataGrid によって取得されるデータを更新することを検討する必要があります。

このページの回答のアプローチが役立つ場合があります: https://stackoverflow.com/a/1581588/1082026

最終的に、コードは次のようになります (注: DataSet の DataTable の行をミラーリングするクラスDataItemを定義する必要があります。具体的には、 DataGridが気にするプロパティを使用します)。

private ObservableCollection<DataItem> dataItems= new ObservableCollection<DataItem>();
public ObservableCollection<DataItem> DataItems
{
    get { return this.dataItems; }
    set 
    { 
         this.dataItems = value; 
         base.OnPropertyChanged("DataItems");
    }
}

このバインディングで

<DataGrid x:Name="dataGrid" ItemsSource="{Binding TestData}".../>

Add() と Remove() を処理したくない場合は、次のようにコレクションを設定します。

IList<DataItem> items = BuildDataGridColumns(cultureDict).Tables[0].AsDataView();
this.DataItems = new ObservableCollection<DataItem>(items);

...

private IList<DataItem> BuildDataGridColumns(Dictionary<string, string> cultureDict,
                                     DataTable additionalDt = null)
{
    // ... here create a list of DataItems from your table, and return it.
}

それが役立つことを願っています!

于 2013-04-17T16:35:58.423 に答える