【问题标题】:The implements bind a collection of object to DataGrid in two-ways实现以两种方式将对象集合绑定到 DataGrid
【发布时间】:2014-12-01 06:56:37
【问题描述】:

我正在尝试在DataGridCollection 之间实现双向绑定。 我想要的是删除我的Collection 中的一个项目会自动导致DataGrid 项目被删除,有没有可能做到?

到目前为止我所做的是:

我的收藏项的代码。

    [XmlRoot("configitem")]
    public class ConfigItem : INotifyPropertyChanged
    {
        private bool bDelete = false;
        [XmlAttribute("name")]
        public string Name { get; set; }
        [XmlAttribute("value")]
        public string Value { get; set; }
        public bool ToBeDelete {
            get
            {
                return bDelete;
            }
            set
            {
                bDelete = value;
                OnPropertyChanged("ToBeDelete");
            }
        }

        [XmlAttribute("description")]
        public string Description { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

XAML 代码是:

<DataGrid.Columns>
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.Header>
            <CheckBox Content="All" x:Name="chkAll" Click="chkAll_Click" />
        </DataGridTemplateColumn.Header>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox Name="chkSelect" IsChecked="{Binding ToBeDelete, 
              Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Margin="15 2 0 0" Click="chkSelect_Click" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTextColumn Width="2*" Binding="{Binding Name}" ClipboardContentBinding="{x:Null}" Header="ConfigName"/>
    <DataGridTextColumn Width="2*" Binding="{Binding Value}" ClipboardContentBinding="{x:Null}" Header="ConfigValue"/>
    <DataGridTextColumn Width="6*" Binding="{Binding Description}" ClipboardContentBinding="{x:Null}" Header="Description"/>
</DataGrid.Columns>

当我单击第一列中的复选框时。 PropertyChanged 事件将被触发。这将使集合项的值发生变化。并且它起作用了。

当我单击删除按钮时。我尝试从当前绑定集合中删除指定的项目。集合项目被删除。但是为什么他们的网格行没有被删除?

public void Delete()
{
    List<ConfigItem> TobeRemovedList = configs.Where(x => x.ToBeDelete.Equals(true)).ToList();
    TobeRemovedList.ForEach(x => configs.Remove(x));
}

我是否需要在删除按钮中再次调用绑定,以便 DataGrid 知道集合已更改?

如果我所做的与最佳实践相去甚远。请告诉我如何。谢谢。

【问题讨论】:

    标签: wpf datagrid


    【解决方案1】:

    是的,有。您需要使用ObservableCollection&lt;T&gt; 而不是List&lt;T&gt;,因为后者不支持更改通知。因此数据绑定引擎不会知道您的列表已更改,因此无法更新DataGrid

    您还需要将ObservableCollection&lt;T&gt; 保留为一个字段,而不是作为一个局部变量一次又一次地创建新的。

    【讨论】:

    • 感谢您的好评和回答。我得到了它 。我需要做的是将我的收藏包装为新类的一个字段。并绑定新的 Class 实例并将正确的字段路径设置为 DataGrid。对 ?因为虽然我没有使用ObservableCollection&lt;T&gt;,但我已经实现了属性更改通知。谢谢。
    • 是的,我可以看到您正在使用名为configs 的东西,而不是您需要将DataGridObservableCollection 绑定。通常,您公开ObservableCollection 类型的属性并将其绑定到DataGrid.ItemsSource。您可以在 SO 或 google 中找到很多示例。如果您需要更多帮助,我很乐意提供帮助。
    • 它已经帮了我很多。我会按照你给我的方向去做。感谢您的热心帮助!
    • Also you need to keep the ObservableCollection&lt;T&gt; as a field, not as a local variable creating new again and again. 在我的测试中。这个非常重要。 ObservableCollection 应该在窗口中保持相同的实例,否则会导致绑定失败。谢谢。
    猜你喜欢
    • 2014-04-18
    • 1970-01-01
    • 2011-01-02
    • 2021-03-01
    • 1970-01-01
    • 2018-07-20
    • 2016-06-17
    • 2011-03-06
    • 1970-01-01
    相关资源
    最近更新 更多