【问题标题】:Binding a DataTable to a DataGrid in MVVM WPF在 MVVM WPF 中将 DataTable 绑定到 DataGrid
【发布时间】:2013-03-24 09:55:27
【问题描述】:

我对整个 C# .net 很陌生,但我搜索了很多,但找不到如何让它工作。

我的视图中有一个 DataGrid,如下所示:

<DataGrid Name="SettingGrid" ItemsSource="{Binding Path=PluginSettings, Mode=OneWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="True" Margin="224.4,10,10,10"/>

PluginSettings 是一个 DataTable,根据用户的操作动态填充不同的列和行。 PluginSettings 总是最新的,我已经检查了调试模式,列和行总是我想要的。但是视图永远不会更新。 经过一番谷歌搜索,我发现 DataTable 不可枚举,因此无法绑定。我将绑定更改为{Binding Path=PluginSettings.DefaultView。 这样,我可以让行完美地工作,但列却不行。

当我向我的 DataTable 添加一列时,视图永远不会显示它。 如果我正确理解 DefaultView 是什么,这意味着我无法将用户在 Grid 上所做的更改复制到实际的 DataTable 以保存它们,这实际上是我的目标。

我错过了什么吗? 使用 DataGrid 只是一个糟糕的选择吗,我想做的事情有更好的选择吗?

希望我说清楚了,英语不是我的第一语言。 谢谢

【问题讨论】:

    标签: c# .net wpf mvvm


    【解决方案1】:
    • 我必须提到,不鼓励在客户端代码 (WPF) 中使用 System.Data
    • 这包括System.Data.DataSetSystem.Data.DataTable 以及System.Data 命名空间内的任何其他类。
    • 您应该创建适当的数据模型并改用它。
    • IMO,System.Data 是一个服务器端概念,不应该被带到客户端。
    • 例如,在 WinRT 中,它甚至不存在。没有System.Data,因此如果您打算将 WPF 应用程序迁移到 WinRT,您将需要重写大量代码。

    话虽如此,此示例在添加新行和添加新列时都有效:

    <Window x:Class="MiscSamples.DataGridAndDataTable"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="DataGridAndDataTable" Height="300" Width="300">
        <DockPanel>
            <Button Content="Add Column" DockPanel.Dock="Top" Click="AddColumn"/>
            <Button Content="Add Row" DockPanel.Dock="Top" Click="AddRow"/>
            <DataGrid Name="SettingGrid" 
                  ItemsSource="{Binding}" 
                  AutoGenerateColumns="True"/>
        </DockPanel>
    </Window>
    

    代码背后:

      public partial class DataGridAndDataTable : Window
        {
            public DataTable PluginSettings { get; set; }
    
            public DataGridAndDataTable()
            {
                InitializeComponent();
    
                PluginSettings = new DataTable();
    
                PluginSettings.Columns.Add("Name", typeof (string));
                PluginSettings.Columns.Add("Date", typeof(DateTime));
    
                PluginSettings.NewRow();
                PluginSettings.NewRow();
    
                PluginSettings.Rows.Add("Name01", DateTime.Now);
    
                DataContext = PluginSettings;
            }
    
            private void AddColumn(object sender, RoutedEventArgs e)
            {
                PluginSettings.Columns.Add("Age", typeof (int));
                DataContext = null;
                DataContext = PluginSettings;
            }
    
            private void AddRow(object sender, RoutedEventArgs e)
            {
                PluginSettings.Rows.Add("Name01", DateTime.Now);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-02-07
      • 2023-04-02
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2019-12-22
      • 2013-07-28
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多