簡體   English   中英

如何在運行時將數據綁定到DataGrid

[英]How to Bind Data to DataGrid at Run-Time

全部,我有一些測試代碼可用於定義為

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以及DataGrid單元中的文本的XAML是:

<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綁定?

有人建議原因是我的數據沒有實現INotifyCollectionChanged接口,但是由於數據的可INotifyCollectionChanged ,我必須在運行時將數據加載到DataGrid中。

如果數據的結構可以更改,如何將這些數據作為實現INotifyPropertyChanged的類綁定到DataGrid

非常感謝你花時間陪伴。

我認為布拉姆是對的。 (對不起,我還沒有添加注釋的特權。)在您的測試代碼中,您使用了一個ObservableCollection,它實現了INotifyPropertyChanged。 DataGrid將選擇對該集合的更改。 您應該考慮在實際代碼中使用ObservableCollection,並通過它更新數據以使其由綁定的DataGrid拾取,而不是直接通過dataGrid.ItemsSource。

本頁答案中的方法可能會有所幫助: https : //stackoverflow.com/a/1581588/1082026

最后,您的代碼可能看起來像這樣(請注意,您必須定義一個DataItem類,該類將鏡像DataSet的DataTable中的一行,尤其是使用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.
}

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM