【问题标题】:C# WPF : Fail in populating DataGridC# WPF:填充 DataGrid 失败
【发布时间】:2015-12-26 23:18:32
【问题描述】:

我有一个“Seive”类,一个 ObservableCollection seiveData 对象。

        public Seive(String id)
    {
        SeiveIdSize = id;
        Caret = 0.0;
        Percent = 0.0;
        Peices = 0;
        Weight = 0.0;
        Size = 0;
    }

    public String SeiveIdSize   {    get;   set;   }
    public Double Weight { get; set; }
    public Double Percent   {    get;   set;   }
    public Double Caret {    get;   set;   }
    public uint Size    {    get;   set;   }
    public uint Peices  {    get;   set;   }

在我的 xml 中:我有

<DataGrid  Name="serverGrid" ItemsSource="{Binding}" .....>
<DataGrid.Columns>
<DataGridTextColumn Header="SEIVE" Width="Auto" Binding="{Binding Path=SeiveIdSize}" SortDirection="Ascending" />
 <DataGridTextColumn Header="CTS" Width="Auto" Binding="{Binding Path=Caret}" />
 <DataGridTextColumn Header=" % " Width="Auto" Binding="{Binding Path=Percent}" />
 <DataGridTextColumn Header="PCS" Width="Auto" Binding="{Binding Path=Peices}" />
 <DataGridTextColumn Header="WGT" Width="Auto" Binding="{Binding Path=Weight}" />
 <DataGridTextColumn Header="SIZE" Width="Auto" Binding="{Binding Path=Size}" />                                    
 </DataGrid.Columns>

在 window Loaded 事件中,我在 seiveData 中填充了 2 个 seives,但我没有看到任何结果/行。

seivesData.Add(new Seive("+10"));
seivesData.Add(new Seive("+8"));
seivesDataGrid.DataContext = seivesData;

编辑: 按钮事件代码:

        private void saveBtn_Click(object sender, RoutedEventArgs e)
    {
        Seive s1 = new Seive("+2");
        s1.Peices = 100;
        s1.Caret = 0.41;
        s1.Weight = 0.10;
        seivesData.Add(s1);
        Seive s = seivesData[0];
        s.Caret = 0.54;
        s.Weight = 0.32;
        seivesData[0] = s;
        seiveDG.DataContext = seivesData;
    }

我哪里错了?我可以看到新添加的 Seive 所有详细信息,但看不到添加到第 0 个 seive 的插入符号和权重。

【问题讨论】:

  • 听说过INotifyPropertyChanged吗?对于此类需求非常重要的接口。

标签: c# wpf datagrid populate


【解决方案1】:

我在您的 xaml 中看到的基本问题是您设置了 ItemsSource = {binding SeiveData} 如果您将此属性传递到网格的数据上下文中,这是错误的。

下面的代码现在可以工作了。核实。

如果您想将 chagnes 通知到 Seive 类中,那么还必须实现 INotifyPropertyChange 接口。

XAML

<DataGrid Name="serverGrid" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=SeiveIdSize}"
                                Header="SEIVE"
                                SortDirection="Ascending" />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Caret}"
                                Header="CTS" />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Percent}"
                                Header=" % " />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Peices}"
                                Header="PCS" />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Weight}"
                                Header="WGT" />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Size}"
                                Header="SIZE" />
        </DataGrid.Columns>
    </DataGrid> 

躲在后面

 ObservableCollection<Seive> _seiveData;
        public ObservableCollection<Seive> SeiveData
        {
            get { return _seiveData; }
            set { _seiveData = value; }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            SeiveData = new ObservableCollection<Seive>();
            SeiveData.Add(new Seive("+10"));
            SeiveData.Add(new Seive("+8"));

            serverGrid.DataContext = SeiveData;

        }

 public class Seive
    {
        public Seive(String id)
        {
            SeiveIdSize = id;
            Caret = 0.0;
            Percent = 0.0;
            Peices = 0;
            Weight = 0.0;
            Size = 0;
        }

        public String SeiveIdSize { get; set; }
        public Double Weight { get; set; }
        public Double Percent { get; set; }
        public Double Caret { get; set; }
        public uint Size { get; set; }
        public uint Peices { get; set; }
    }

【讨论】:

  • 谢谢。我在我的 Seive 类中实现了 INotifyPropertyChange,它起作用了。再次感谢。
【解决方案2】:

您提到您正在使用列表,但要使数据绑定起作用,您需要使用ObservableCollection&lt;T&gt;,如果集合发生更改,它将通知数据网格。

编辑:

正如 cmets 中所指出的,这与变化无关。您已经将数据上下文设置为列表,因此您的数据上下文中不会有seiveData。相反,请尝试以下操作:

ItemsSource="{Binding}"

如果您仍需要通知您的视图更改,请考虑ObservableCollection&lt;T&gt;,如上所述。

【讨论】:

  • 但问题不在于变化,问题在于填充数据网格。
  • 我将 List 改为 ObservableCollection,但没有区别。
  • 在 Loaded 事件中,当您提到时,我将 Datacontext 替换为 ItemSource,是的,我可以看到数据。但我的观点不仅仅是添加,我也可以动态添加/更改。在 btn 点击中,我添加了一个新的 Seive 并更改了 0th seive 的一些值 & 称为 .DataContenxt = seivesData;。新添加的 seive 在数据网格中已更新/可见,但未刷新第 0 个对象的更新值。为什么会这样?
  • 我不建议从代码中设置ItemsSource,但我建议更改您在 XAML 中的绑定。从代码设置 DataContext 并使用 ItemsSource="{Binding}" 或从代码设置 ItemsSource,不要混淆它们。对于刷新,ObservableCollection&lt;T&gt; 就足够了。
  • 好的,正如你建议的那样,在 xml 中我有 ItemsSource="{Binding}" 并且在添加和更改值后加载和 btn 事件中我调用 Datacontext = seivesData。但是,结果是相同的,即显示新添加的,但不刷新现有对象的更新值。我的 seivesData 也是 ObservableCollection seivesData;数据类型。在我的问题中显示了 btn 事件代码。也许这可能会帮助你支持我。谢谢。
【解决方案3】:

可能 DataGrid 所在的容器的 DataContext 未设置为服务于seiveData-property 的对象。或者您的 seiveData-list 甚至没有属性支持,而只是一个字段。这是不允许的。即使字段(成员变量)被声明为 public,绑定引擎也不会绑定到它——它必须由属性支持。

还请注意,如果您在 Loaded-event 中填写列表,seiveList 必须是 INotifyCollectionChanged-implementing 集合,例如 ObservableCollection&lt;T&gt;,因为绑定是在填充列表之前完成的。否则,DataGrid 将不知道新数据已插入到列表中。

【讨论】:

    【解决方案4】:

    每个人都说的一种方法是将其绑定到ObservableCollection

    另一种方法是从 DataTable 绑定。

    使用

    将您的列表转换为 DataTable

    How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow

    Creating a DataTable From a Query (LINQ to DataSet)

    我今天发现了这个。非常有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      相关资源
      最近更新 更多