- Java锁的逻辑(结合对象头和ObjectMonitor)
- 还在用饼状图?来瞧瞧这些炫酷的百分比可视化新图形(附代码实现)⛵
- 自动注册实体类到EntityFrameworkCore上下文,并适配ABP及ABPVNext
- 基于Sklearn机器学习代码实战
WPF入门教程系列五——Window 介绍 。
7.上面Buttom的Command类就是纯命令,什么参数都不接收,这次的ProvinceChangedCommand类在执行命令的时候,能够传参数!采用泛型的形式,给 Action 添加泛型参数.
8. 在Visual Studio 2022的解决方案资源管理器中,使用鼠标右键单击“Command”文件夹,在弹出菜单中选择“添加--> 类”,在弹出的“添加新项”对话框中,选择添加 “ProvinceChangedCommand”类,这是一个我们要实现的保存操作指令,然后选择“添加”。ProvinceChangedCommand的具体代码如下:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Input;
namespace
WpfGridDemo.NET7.Command {
public
class
ProvinceChangedCommand<T>
: ICommand {
///
<summary>
///
命令能否执行
///
</summary>
readonly
Func<
bool
>
_canExecute;
///
<summary>
///
命令执行的方法
///
</summary>
readonly
Action<T>
_execute;
///
<summary>
///
命令的构造函数
///
</summary>
///
<param name="action">
命令需执行的方法
</param>
///
<param name="canExecute">
命令是否可以执行的方法
</param>
public
ProvinceChangedCommand(Action<T> action, Func<
bool
>
canExecute) { _execute
=
action; _canExecute
=
canExecute; }
///
<summary>
///
判断命令是否可以执行
///
</summary>
///
<param name="parameter"></param>
///
<returns></returns>
public
bool
CanExecute(Object parameter) {
if
(_canExecute ==
null
)
return
true
;
return
_canExecute(); }
///
<summary>
///
执行命令
///
</summary>
///
<param name="parameter"></param>
public
void
Execute(Object parameter) { _execute((T)parameter); }
///
<summary>
///
事件追加、移除
///
</summary>
public
event
EventHandler CanExecuteChanged { add {
if
(_canExecute !=
null
) CommandManager.RequerySuggested
+=
value; } remove {
if
(_canExecute !=
null
) CommandManager.RequerySuggested
-=
value; } } } }
void
ProviceSelectionChangedExecute(
object
sender) {
try
{
if
(sender
is
ComboBox) { ComboBox drp
=sender
as
ComboBox; ProvinceCode
=
drp.SelectedValue.ToString(); GridDbContext db
=
new
GridDbContext();
var
list =
db.City.AsTracking().ToList(); List
<City> citys = list.Where(x => x.ProvinceCode ==
ProvinceCode).ToList(); cityList
=
new
ObservableCollection<City>
();
if
(citys !=
null
) { citys.ForEach((t)
=>
{ cityList.Add(t); } ); }
var
cityCodes =
from
city
in
citys
select
city.Code; List
<Area> areas = db.Area.AsTracking().ToList().Where(
x =>
cityCodes.Contains(x.CityCode)).ToList(); areaList
=
new
ObservableCollection<Area>
();
if
(areas!=
null
) { areas.ForEach((t)
=>
{ areaList.Add(t); } ); } } }
catch
(Exception ex) {
throw
ex; } }
结果如图:我们看到了省份下拉框中已经了省份信息.
。
9.通过绑定依赖属性,实现自动刷新需要实现以下三步:
1.Model继承并实现 INotifyPropertyChanged 接口; 。
2.数据集合使用ObservableCollection<T>集合; 。
3.View使用Binding数据对象属性,
如果不行再看看集合在赋值前需要实例化,不然就出不来(必须要同一个源才行) 。
10. 在Visual Studio 2022中打开MainWindows.xmal文件,并将文件中的代码修改成如下:
<
Window
x:Class
="WpfGridDemo.NET7.MainWindow"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
xmlns:be
="http://schemas.microsoft.com/xaml/behaviors"
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local
="clr-namespace:WpfGridDemo.NET7"
mc:Ignorable
="d"
Title
="MainWindow"
Height
="600"
Width
="960"
Loaded
="Window_Loaded"
>
<
Grid
>
<
Grid.RowDefinitions
>
<
RowDefinition
Height
="100"
></
RowDefinition
>
<
RowDefinition
Height
="*"
></
RowDefinition
>
<
RowDefinition
Height
="25"
></
RowDefinition
>
</
Grid.RowDefinitions
>
<
WrapPanel
Grid.Row
="0"
HorizontalAlignment
="Left"
>
<
ComboBox
x:Name
="cboProvince"
DisplayMemberPath
="Name"
SelectedValuePath
="Code"
>
<
be:Interaction.Triggers
>
<
be:EventTrigger
EventName
="SelectionChanged"
>
<
be:InvokeCommandAction
Command
="
{Binding ProviceChangedAction}
"
CommandParameter
="
{Binding ElementName=cboProvince}
"
/>
</
be:EventTrigger
>
</
be:Interaction.Triggers
>
</
ComboBox
>
</
WrapPanel
>
<
DataGrid
x:Name
="gridArea"
Grid.Row
="1"
ItemsSource
="
{Binding GridAreaList}
"
AutoGenerateColumns
="False"
HorizontalAlignment
="Left"
VerticalAlignment
="Top"
SelectedItem
="
{Binding Path=AreaVM, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}
"
>
<
DataGrid.Columns
>
<
DataGridComboBoxColumn
Header
="城市"
Width
="120"
ItemsSource
="
{Binding Path=DataContext.GridCityList,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}
"
x:Name
="cboCity"
ClipboardContentBinding
="
{x:Null}
"
SelectedValuePath
="Code"
SelectedValueBinding
="
{Binding Path=CityCode,
UpdateSourceTrigger=PropertyChanged}
"
DisplayMemberPath
="Name"
SelectedItemBinding
="
{x:Null}
"
/>
<
DataGridTextColumn
Header
="县区镇"
Width
="*"
Binding
="
{Binding Name}
"
ClipboardContentBinding
="
{x:Null}
"
/>
<
DataGridTextColumn
Header
="邮编"
Width
="100"
Binding
="
{Binding Code}
"
ClipboardContentBinding
="
{x:Null}
"
/>
<
DataGridTextColumn
Header
="创建时间"
Width
="160"
Binding
="
{Binding Created}
"
ClipboardContentBinding
="
{x:Null}
"
/>
<
DataGridTextColumn
Header
="更新时间"
Width
="160"
Binding
="
{Binding Updated}
"
ClipboardContentBinding
="
{x:Null}
"
/>
</
DataGrid.Columns
>
</
DataGrid
>
<
WrapPanel
Grid.Row
="2"
>
<
Button
x:Name
="btnRefresh"
Height
="22"
Width
="120"
Click
="btnRefresh_Click"
>
刷新
</
Button
>
<
Button
x:Name
="btnSave"
Height
="22"
Width
="120"
Command
="
{Binding ClickSaveAction}
"
>
保存
</
Button
>
</
WrapPanel
>
</
Grid
>
</
Window
>
。
using
Microsoft.EntityFrameworkCore;
using
System;
using
System.Collections.Generic;
using
System.Collections.ObjectModel;
using
System.ComponentModel;
using
System.DirectoryServices.ActiveDirectory;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Controls;
using
System.Windows.Input;
using
WpfGridDemo.NET7.Entitys;
namespace
WpfGridDemo.NET7.ViewModel {
public
class
MainWindowVM: ViewModelBase {
public
MainWindowVM() { cityList
=
new
ObservableCollection<City>
(); areaList
=
new
ObservableCollection<Area>
(); }
private
Area m_Area;
///
<summary>
///
县镇区数据
///
</summary>
public
Area AreaVM {
get
{
return
m_Area; }
set
{ m_Area =
value; } }
private
string
m_Province_Code;
///
<summary>
///
省--代码
///
</summary>
public
string
ProvinceCode {
get
=> m_Province_Code;
set
=> m_Province_Code =
value; }
private
ObservableCollection<Area>
areaList;
public
ObservableCollection<Area>
GridAreaList {
get
{
return
areaList; }
set
{ areaList
=
value; RaisePropertyChanged(
"
GridAreaList
"
); } }
private
ObservableCollection<City>
cityList;
public
ObservableCollection<City>
GridCityList {
get
{
return
cityList; }
set
{ cityList
=
value; RaisePropertyChanged(
"
GridCityList
"
); } }
///
<summary>
///
命令要执行的方法
///
</summary>
void
SaveExecute() {
try
{ GridDbContext db
=
new
GridDbContext();
var
list=
db.Area.AsTracking().ToList(); Area modifyArea
= list.Where(x=>x.Id==
AreaVM.Id).FirstOrDefault();
if
(modifyArea !=
null
) { modifyArea.Name
=
AreaVM.Name; modifyArea.Updated
=
DateTime.Now; db.SaveChanges(); } }
catch
(Exception ex) {
throw
ex; } }
///
<summary>
///
命令是否可以执行
///
</summary>
///
<returns></returns>
bool
CanSaveExecute() {
return
false
; }
///
<summary>
///
创建新命令
///
</summary>
public
ICommand ClickSaveAction {
get
{
return
new
Command.SaveCommand(SaveExecute, CanSaveExecute); } }
//
combobox
///
<summary>
///
命令要执行的方法
///
</summary>
void
ProviceSelectionChangedExecute(
object
sender) {
try
{
if
(sender
is
ComboBox) { ComboBox drp
=sender
as
ComboBox; ProvinceCode
=
drp.SelectedValue.ToString(); GridDbContext db
=
new
GridDbContext();
var
list =
db.City.AsTracking().ToList(); List
<City> citys = list.Where(x => x.ProvinceCode ==
ProvinceCode).ToList();
var
cityCodes =
from
city
in
citys
select
city.Code; List
<Area> areas = db.Area.AsTracking().ToList().Where(
x =>
cityCodes.Contains(x.CityCode)).ToList(); areaList.Clear();
if
(areas!=
null
) { areas.ForEach((t)
=>
{ areaList.Add(t); } ); } cityList.Clear();
if
(citys !=
null
) { citys.ForEach((t)
=>
{ cityList.Add(t); } ); } } }
catch
(Exception ex) {
throw
ex; } }
///
<summary>
///
命令是否可以执行
///
</summary>
///
<returns></returns>
bool
CanSelectionChangedExecute() {
return
true
; }
///
<summary>
///
创建新命令
///
</summary>
public
ICommand ProviceChangedAction {
get
{
return
new
Command.ProvinceChangedCommand<
object
>
(ProviceSelectionChangedExecute, CanSelectionChangedExecute); } } }
12.在Visual Studio 2022中按F5键,启动WPF应用程序。然后使用鼠标点击省份下拉框,能够看到,界面中DataGrid中的数据,随着下拉框的变化而随之变化。如下图.
。
最后此篇关于WPF入门教程系列二十八——DataGrid使用示例MVVM模式(6)的文章就讲到这里了,如果你想了解更多关于WPF入门教程系列二十八——DataGrid使用示例MVVM模式(6)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
对此感到疯狂,真的缺少一些东西。 我有webpack 4.6.0,webpack-cli ^ 2.1.2,所以是最新的。 在文档(https://webpack.js.org/concepts/mod
object Host "os.google.com" { import "windows" address = "linux.google.com" groups = ["linux"] } obj
每当我安装我的应用程序时,我都可以将数据库从 Assets 文件夹复制到 /data/data/packagename/databases/ .到此为止,应用程序工作得很好。 但 10 或 15 秒后
我在 cc 模式缓冲区中使用 hideshow.el 来折叠我不查看的文件部分。 如果能够在 XML 文档中做到这一点就好了。我使用 emacs 22.2.1 和内置的 sgml-mode 进行 xm
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
根据java: public Scanner useDelimiter(String pattern) Sets this scanner's delimiting pattern to a patt
我读过一些关于 PRG 模式以及它如何防止用户重新提交表单的文章。比如this post有一张不错的图: 我能理解为什么在收到 2xx 后用户刷新页面时不会发生表单提交。但我仍然想知道: (1) 如果
看看下面的图片,您可能会清楚地看到这一点。 那么如何在带有其他一些 View 的简单屏幕中实现没有任何弹出/对话框/模式的微调器日期选择器? 我在整个网络上进行了谷歌搜索,但没有找到与之相关的任何合适
我不知道该怎么做,我一直遇到问题。 以下是代码: rows = int(input()) for i in range(1,rows): for j in range(1,i+1):
我想为重写创建一个正则表达式。 将所有请求重写为 index.php(不需要匹配),它不是以/api 开头,或者不是以('.html',或'.js'或'.css'或'.png'结束) 我的例子还是这样
MVC模式代表 Model-View-Controller(模型-视图-控制器) 模式 MVC模式用于应用程序的分层开发 Model(模型) - 模型代表一个存取数据的对象或 JAVA PO
我想为组织模式创建一个 RDF 模式世界。您可能知道,组织模式文档基于层次结构大纲,其中标题是主要的分组实体。 * March auxiliary :PROPERTIES: :HLEVEL: 1 :E
我正在编写一个可以从文件中读取 JSON 数据的软件。该文件包含“person”——一个值为对象数组的对象。我打算使用 JSON 模式验证库来验证内容,而不是自己编写代码。符合代表以下数据的 JSON
假设我有 4 张 table 人 公司 团体 和 账单 现在bills/persons和bills/companys和bills/groups之间是多对多的关系。 我看到了 4 种可能的 sql 模式
假设您有这样的文档: doc1: id:1 text: ... references: Journal1, 2013, pag 123 references: Journal2, 2014,
我有这个架构。它检查评论,目前工作正常。 var schema = { id: '', type: 'object', additionalProperties: false, pro
这可能很简单,但有人可以解释为什么以下模式匹配不明智吗?它说其他规则,例如1, 0, _ 永远不会匹配。 let matchTest(n : int) = let ran = new Rand
我有以下选择序列作为 XML 模式的一部分。理想情况下,我想要一个序列: 来自 my:namespace 的元素必须严格解析。 来自任何其他命名空间的元素,不包括 ##targetNamespace和
我希望编写一个 json 模式来涵盖这个(简化的)示例 { "errorMessage": "", "nbRunningQueries": 0, "isError": Fals
首先,我是 f# 的新手,所以也许答案很明显,但我没有看到。所以我有一些带有 id 和值的元组。我知道我正在寻找的 id,我想从我传入的三个元组中选择正确的元组。我打算用两个 match 语句来做到这
我是一名优秀的程序员,十分优秀!