【问题标题】:How to Bind Data to DataGrid at Run-Time如何在运行时将数据绑定到 DataGrid
【发布时间】:2013-04-10 09:28:21
【问题描述】:

所有,我有一些测试代码可以处理一些定义为

的测试数据
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}" 不存在,更新用于处理测试数据的网格视觉效果的代码不再起作用。应该绑定插入到TextBoxDataGrid 单元格中的文本的 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接口,但是由于数据的可变性,我必须在运行时将数据加载到DataGrid中。

如果数据的结构可以更改,我如何将此数据绑定到 DataGrid 作为实现 INotifyPropertyChanged 的类?

非常感谢您的宝贵时间。

【问题讨论】:

    标签: c# wpf binding datagrid inotifycollectionchanged


    【解决方案1】:

    我认为 Blam 是对的。 (抱歉,我还没有添加 cmets 的权限。)在您的测试代码中,您使用 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.
    }
    

    希望有帮助!

    【讨论】:

    • 这是一个很好的答案,也是我昨晚意识到并开始做的事情。非常感谢您的宝贵时间...
    猜你喜欢
    • 2013-05-28
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2012-07-05
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    相关资源
    最近更新 更多