WPF:数据绑定--EditingCollections绑定的编辑集合

融资困难
• 阅读 2152

EditingCollections绑定的编辑集合

实现效果:
如图中说明
WPF:数据绑定--EditingCollections绑定的编辑集合

WPF:数据绑定--EditingCollections绑定的编辑集合

实践:

  1. IEditableObject接口的使用
  2. IEditableCollectionView的使用
  3. 对集合的增、删、改编辑行为的应用

代码:
界面xaml

<ListView Name="itemsControl"  ItemsSource="{StaticResource MyData}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Item"
                  DisplayMemberBinding="{Binding Path=Description}"/>
            <GridViewColumn Header="Price"
                  DisplayMemberBinding="{Binding Path=Price, StringFormat=c}"/>
        </GridView>
    </ListView.View>
</ListView>

编辑按钮代码:

  1. 检查选择项是否为空
  2. 获取集合的可编辑集合视图
  3. 开始指定项的编辑事务
  4. 新建一个编辑窗口,其绑定源为选定项
  5. 窗口返回时,检查确认后进行结束编辑事务并保存挂起的更改。否则还原项的原始值,结束编辑事务
private void Edit_Click(object sender, RoutedEventArgs e)
{
    if (itemsControl.SelectedItem == null)
    {
        MessageBox.Show("No item is selected");
        return;
    }

    var editableCollectionView =
        itemsControl.Items as IEditableCollectionView;

    // Create a window that prompts the user to edit an item.
    var win = new ChangeItem();
    editableCollectionView.EditItem(itemsControl.SelectedItem);
    win.DataContext = itemsControl.SelectedItem;

    // If the user submits the new item, commit the changes.
    // If the user cancels the edits, discard the changes. 
    if ((bool) win.ShowDialog())
    {
        editableCollectionView.CommitEdit();
    }
    else
    {
        editableCollectionView.CancelEdit();
    }
}

添加按钮代码:

  1. 创建一个窗口提示使用者输入新项,窗口绑定源为集合新项 editableCollectionView.AddNew()
  2. 检查窗口返回值,进行对应结束添加事务并保存挂起新项,否则放弃新项
private void Add_Click(object sender, RoutedEventArgs e)
{
    var editableCollectionView = itemsControl.Items as IEditableCollectionView;

    if (!editableCollectionView.CanAddNew)
    {
        MessageBox.Show("You cannot add items to the list.");
        return;
    }

    // Create a window that prompts the user to enter a new
    // item to sell.
    var win = new ChangeItem {DataContext = editableCollectionView.AddNew()};

    //Create a new item to be added to the collection.

    // If the user submits the new item, commit the new
    // object to the collection.  If the user cancels 
    // adding the new item, discard the new item.
    if ((bool) win.ShowDialog())
    {
        editableCollectionView.CommitNew();
    }
    else
    {
        editableCollectionView.CancelNew();
    }
}

移除按钮代码:

  1. 检查选定项、获取可编辑集合视图,检查是否可以移除操作,消息盒提示是否确定移除
  2. 确认后视图移除当前项
private void Remove_Click(object sender, RoutedEventArgs e)
{
    var item = itemsControl.SelectedItem as PurchaseItem;

    if (item == null)
    {
        MessageBox.Show("No Item is selected");
        return;
    }

    var editableCollectionView =
        itemsControl.Items as IEditableCollectionView;

    if (!editableCollectionView.CanRemove)
    {
        MessageBox.Show("You cannot remove items from the list.");
        return;
    }

    if (MessageBox.Show("Are you sure you want to remove " + item.Description,
        "Remove Item", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
    {
        editableCollectionView.Remove(itemsControl.SelectedItem);
    }
}

IEditableObject接口代码:

  1. 本示例需要一个结构数据:
  2. 两个数据字段(对前后顺序有要求?)
private struct ItemData
{
    internal string Description;
    internal DateTime OfferExpires;
    internal double Price;
}
private ItemData _copyData;
private ItemData _currentData;
#region IEditableObject Members

public void BeginEdit()
{
    _copyData = _currentData;
}

public void CancelEdit()
{
    _currentData = _copyData;
    NotifyPropertyChanged("");
}

public void EndEdit()
{
    _copyData = new ItemData();
}

#endregion

扩展:

  1. IEditableObject 接口:提供提交或回滚对用作数据源的对象所做更改的功能。
  2. 当集合视图实现 IEditableCollectionView 接口时,如果允许更改基础集合,则可以使用 IEditableCollectionView 公开的方法和属性直接更改基础集合,而不用考虑集合的类型。
  3. 类型 ItemCollection、 BindingListCollectionView 和 ListCollectionView 是WPF附带的类型,这些类型继承自 CollectionView。这些类型还将实现 IEditableCollectionView,因此您可以编辑使用这些类型之一的集合。特别是经常会使用 ItemCollection,因为 ItemsControl.Items 属性为 ItemCollection。
  4. 具体内部实现框架待研再给出示例。。。
点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
7个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Easter79 Easter79
3年前
SwiftUI 跨组件数据传递
作者:Cyandev,iOS和MacOS开发者,目前就职于字节跳动0x00前言众所周知,SwiftUI的开发模式与React、Flutter非常相似,即都是声明式UI,由数据驱动(产生)视图,视图也会与数据自动保持同步,框架层会帮你处理“绑定”的问题。在声明式UI中不存在命令式地让一个视图变成xxx
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这