主从结构在企业级应用中相当常见,这里结合我的例子谈一下wpf中主从结构列表展示的常用做法,具体效果见 wpf企业级开发中的几种常见业务场景

  首先,Model有两种,主表对应model(假设为modelA),从表对应的model(假设为modelB),两种model分别用于绑定列表,就是普通列表的绑定。

  其次,由于要实现联动效果(即选择主表中的一条记录显示从表的记录),故而我们的ViewModel里面必须设计一个SelectedModelA用来绑定选中项,SelectedModelA变化时去更新modelB列表的数据源(通常SelectedModelA中会包含一个集合,不过我这里由于其他原因单独弄了个集合,逻辑其实大同小异)。

  下面是我的代码,由于夹杂着一些业务,仅供参考,其实读者只需明白主表的选中项作为从表UI的数据源即可。

  UI部分,可先直接看下具体效果 wpf企业级开发中的几种常见业务场景,绑定部分主要看两个DataGrid即可。

    <Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<HeaderedContentControl>
<HeaderedContentControl.Header>
<DockPanel>
<TextBlock VerticalAlignment="Center" Text="{DynamicResource ProductClassify_Header_Classify}" DockPanel.Dock="Left"/>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Right" Orientation="Horizontal">
<Button Name="Button_RefreshClassify" Style="{StaticResource RefreshIconButton}" Content="{DynamicResource Common_Refresh}" Command="{Binding TreeVM.RefreshCommand}"/>
</StackPanel>
</DockPanel>
</HeaderedContentControl.Header>
<local:ClassifyTreeControl DataContext="{Binding TreeVM}"/>
</HeaderedContentControl> <HeaderedContentControl Name="Header_Product" Grid.Column="1">
<HeaderedContentControl.Header>
<DockPanel>
<TextBlock VerticalAlignment="Center" Text="{DynamicResource ProductList_Header_Product}" DockPanel.Dock="Left"/>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Right" Orientation="Horizontal">
<Button Name="Button_RefreshClassify2" Style="{StaticResource RefreshIconButton}" Content="{DynamicResource Common_Refresh}" Command="{Binding RefreshCommand}"/>
<Button Name="Button_AddProduct" Visibility="{Binding AddButtonVisibility}" IsEnabled="{Binding CanAdd}" Style="{StaticResource AddIconButton}" Content="{DynamicResource Common_Add}" Click="Button_AddProduct_Click"/>
<Button Name="Button_ModifyProduct" Visibility="{Binding ModifyButtonVisibility}" IsEnabled="{Binding CanModify}" Style="{StaticResource ModifyIconButton}" Content="{DynamicResource Common_Modify}" Click="Button_ModifyProduct_Click"/>
<Button Name="Button_DeleteProduct" Visibility="{Binding DeleteButtonVisibility}" Style="{StaticResource DeleteIconButton}" Content="{DynamicResource Common_Delete}" Command="{Binding DeleteCommand}"/>
</StackPanel>
</DockPanel>
</HeaderedContentControl.Header>
<DataGrid Name="DataGrid_Product" ItemsSource="{Binding Products}" SelectedItem="{Binding SelectedProduct}" SelectionMode="Single" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="{DynamicResource Product_Num}" Binding="{Binding Num}" Width="120"/>
<DataGridTextColumn Header="{DynamicResource Product_Name_CH}" Binding="{Binding Name_CH}" Width="100"/>
<DataGridTextColumn Header="{DynamicResource Product_Name_EN}" Binding="{Binding Name_EN}" Width="100"/>
<DataGridTextColumn Header="{DynamicResource Product_Unit}" Binding="{Binding Unit}" Width="50"/>
<DataGridTextColumn Header="{DynamicResource Product_Weight}" Binding="{Binding Weight}" Width="100"/>
<DataGridTextColumn Header="{DynamicResource Product_Size}" Binding="{Binding Size}" Width="100"/>
<DataGridTemplateColumn Header="{DynamicResource Product_CanSale}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsThreeState="False" IsChecked="{Binding CanSale}" IsEnabled="False" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" Width="50"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{DynamicResource Product_ReferencePrice}" Binding="{Binding ReferencePrice}" Width="100"/>
<DataGridTemplateColumn Header="{DynamicResource Product_StopProduction}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsThreeState="False" IsChecked="{Binding StopProduction}" IsEnabled="False" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" Width="50"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{DynamicResource Product_MinSaleCount}" Binding="{Binding MinSaleCount}" Width="80"/>
<DataGridTextColumn Header="{DynamicResource Product_PackageCount}" Binding="{Binding PackageCount}" Width="80"/>
<DataGridTextColumn Header="{DynamicResource Product_Remark}" Binding="{Binding Remark}" Width="200"/>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
<Setter Property="ToolTip">
<Setter.Value>
<Border Width="100" Height="100" BorderBrush="#FF7DB6D8" BorderThickness="1" Padding="1">
<Border.Resources>
<product:PicUrlConverter x:Key="urlConverter"/>
</Border.Resources>
<Image Source="{Binding ProductPic, Converter={StaticResource urlConverter}}"/>
</Border>
</Setter.Value>
</Setter>
<EventSetter Event="MouseDoubleClick" Handler="Button_ModifyProduct_Click"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>
</HeaderedContentControl>
</Grid>

  ViewModel中相关代码

    public class ProductListVM : ViewModelBase
{
public ProductListVM()
{
LoadData();
TreeVM.SelectedChanged += (s, e) =>
{
LoadData();
if (TreeVM.SelectedModel != null && !string.IsNullOrEmpty(TreeVM.SelectedModel.ID))
CanAdd = true;
else
CanAdd = false;
};
} private ClassifyTreeVM _treeVM;
public ClassifyTreeVM TreeVM
{
get
{
return _treeVM ?? (_treeVM = new ClassifyTreeVM());
}
} private tb_product _selectedProduct;
public tb_product SelectedProduct
{
get
{
return _selectedProduct;
}
set
{
_selectedProduct = value;
OnPropertyChanged("SelectedProduct");
if (value != null)
CanModify = true;
else
CanModify = false;
RaiseCanExecute();
}
} private List<tb_product> _products;
public List<tb_product> Products
{
get
{
return _products;
}
set
{
_products = value;
OnPropertyChanged("Products");
}
} protected override void LoadData()
{
if (TreeVM.SelectedModel != null && !string.IsNullOrEmpty(TreeVM.SelectedModel.ID))
Products = XDBContext.tb_product.Where(p => p.ClassifyID == TreeVM.SelectedModel.ID).AsNoTracking().ToList();
else
Products = XDBContext.tb_product.AsNoTracking().ToList();
} }
  public class ClassifyTreeVM : ViewModelBase
{
public ClassifyTreeVM()
{
LoadData();
} private BindingList<TB_ClassifyTreeModel> _classifyModels;
public BindingList<TB_ClassifyTreeModel> ClassifyModels
{
get
{
return _classifyModels;
}
set
{
_classifyModels = value;
OnPropertyChanged("ClassifyModels");
}
} public event EventHandler SelectedChanged; private TB_ClassifyTreeModel _selectedModel;
public TB_ClassifyTreeModel SelectedModel
{
get
{
return _selectedModel;
}
set
{
_selectedModel = value;
OnPropertyChanged("SelectedModeld");
if (SelectedChanged != null)
SelectedChanged(SelectedModel, null);
}
} protected override void LoadData()
{
ClassifyModels = new BindingList<TB_ClassifyTreeModel>();
ClassifyModels.Add(new TB_ClassifyTreeModel(XDBContext)
{
ClassifyName = "All",
ID = string.Empty
});
}
}

  关于主从结构编辑的保存,UI绑定就是一个主表model(包含从表集合),没什么特别的。只是在保存的时候一起保存从表信息即可(从表中可能有增删改,具体如何操作取决于数据操作层,数据少懒惰点的做法可以一股脑先将相关从表记录全删掉然后添加,这样就不用区分哪些记录是删除的、哪些是修改的及哪些是新添加的;通常有时候会在model中设计一个状态属性,以方便区分model的状态)

wpf企业应用之主从结构列表的更多相关文章

  1. wpf企业应用之SelectButton(用于列表页之类的选择)

    在企业级应用中,通常我们会遇到这样的需求,需要点击一个按钮选择列表中的一项或者多项,然后将结果显示到按钮中.这里我给自己的控件命名为SelectButton,具体效果见 wpf企业级开发中的几种常见业 ...

  2. WPF如何用TreeView制作好友列表、播放列表

    WPF如何用TreeView制作好友列表.播放列表 前言 TreeView这个控件对于我来说是用得比较多的,以前做的小聊天软件(好友列表).音乐播放器(播放列表).类库展示器(树形类结构)等都用的是T ...

  3. Angular2.js——主从结构

    学习这一篇的内容,还回到我们快速起步的应用上来. 主从结构,我们想要做一个英雄的列表,我们希望用户在列表中选中一个英雄,然后这个被选中的英雄出现在详情视图中.主视图是英雄列表,从视图则是被选中英雄的详 ...

  4. WPF如何用TreeView制作好友列表、播放列表(转)

    WPF如何用TreeView制作好友列表.播放列表 前言 TreeView这个控件对于我来说是用得比较多的,以前做的小聊天软件(好友列表).音乐播放器(播放列表).类库展示器(树形类结构)等都用的是T ...

  5. DWZ主从结构计算

    最终效果图: 首先我们需要修改一下主从结构的源码dwz.database.js,如下: function tdHtml(field){ var html='',suffix=''; if(field. ...

  6. MySql5.5以上版本设置主从结构的例子

    为了实现读写分离,一般都需要先设置好mysql的主从结构,网上现有的mysql配置大都基于低版本,在5.5以上版本无法配置成功,所以参考了官方文档,写了这篇笔记. *主要参考Mysql 5.6的官方文 ...

  7. WPF - Group分组对ListBox等列表样式的约束

    原文:WPF - Group分组对ListBox等列表样式的约束 在做WPF主题支持时,出现一个分组引起的莫名错误,可是折腾了我一番.在没有使用样式时,列表分组很正常,使用了别人写的ListBox列表 ...

  8. 为 WPF 程序添加 Windows 跳转列表的支持

    原文:为 WPF 程序添加 Windows 跳转列表的支持 Windows 跳转列表是自 Windows 7 时代就带来的功能,这一功能是跟随 Windows 7 的任务栏而发布的.当时应用程序要想用 ...

  9. Redis主从结构主节点执行写入后wait命令对性能的影响

    这里的Redis主从结构可以是简单的主从,sentinel,redis cluster中的主从等. wait命令的作用:此命令将阻塞当前客户端,直到当前Session连接(主节点上)所有的写命令都被传 ...

随机推荐

  1. SpringCloud之Eureka(注册中心集群篇)

    一:集群环境搭建 第一步:我们新建两个注册中心工程一个叫eureka_register_service_master,另一个叫eureka_register_service_backup eureka ...

  2. D. GCD Counting(树上dp)

    题目链接:http://codeforces.com/contest/1101/problem/D 题目大意:给你n个点,每个点都有权值,然后给你边的关系,问你树上的最大距离.(这里的最大距离指的是这 ...

  3. P1879 [USACO06NOV]玉米田Corn Fields (状压dp入门)

    题目链接: https://www.luogu.org/problemnew/show/P1879 具体思路: 我们可以先把所有合法的情况枚举出来,然后对第一行判断有多少种情况满足,然后对于剩下的行数 ...

  4. 【Python】POST上传APK检测是否存在ZipperDown漏洞

    前言 用POST的方式上传文件,检测APK是否存在ZipperDown漏洞. 代码 # authour:zzzhhh # 2018.08.08 # check ZipperDown # -*- cod ...

  5. 【HASPDOG】hasp_update参数f和i区别

    [root@BICServer-TX shared]# ./hasp_update This is a simple demo program for the Sentinel Update and ...

  6. 搭建本地git服务器

    最近因为项目需求,需要实现一个原型系统,加上后期项目需要多人协作,考虑采用了git做版本控制. 这里主要简要描述下git服务器和客户端的搭建和配置. 1.git服务器 (1)安装git sudo ap ...

  7. 两个Bounding Box的IOU计算代码

    Bounding Box的数据结构为(xmin,ymin,xmax,ymax) 输入:box1,box2 输出:IOU值 import numpy as np def iou(box1,box2): ...

  8. http://code52.org/DownmarkerWPF/

    http://code52.org/DownmarkerWPF/ http://kb.cnblogs.com/page/132209/

  9. Struts 2 Overview

    Struts2 is popular and mature web application framework based on the MVC design pattern. Struts2 is ...

  10. Linux系统的优势

    熟悉电脑的人都知道,Linux 相比较于 Windows 有着众多的优势,所以现在越来越多的电脑用户开始使用 Linux 进行办公.学习.总体来讲,Linux 的优势主要有以下几个方面. 一.开源.免 ...