1

我有一个“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>

在窗口加载事件中,我在 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 的插入符号和权重。

4

4 回答 4

2

我在您的 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; }
    }
于 2012-05-17T10:51:02.507 回答
1

You mention you're using a list, but for databinding to work, you would need to use ObservableCollection<T> which will notify the datagrid if the collection changes.

EDIT:

As pointed out in the comments, it's not about changes. You're already setting your datacontext to the list, so there won't be a seiveData in your DataContext. Instead, try the following:

ItemsSource="{Binding}"

If you still need to notify your view of changes, consider ObservableCollection<T> as has been mentioned.

于 2012-05-17T10:30:53.030 回答
0

正如每个人所说的一种方法是将其绑定到ObservableCollection.

另一种方法是从 DataTable 绑定。

使用将您的列表转换为 DataTable

如何:在通用类型 T 不是 DataRow 的情况下实现 CopyToDataTable

从查询创建数据表 (LINQ to DataSet)

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

于 2012-05-17T10:42:35.663 回答
0

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

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

于 2012-05-17T10:35:38.833 回答