wpf中有validateRule类, 用于界面元素的验证, 如何后台去控制validateRule呢?
1. UI层要binding写好的ValidateRule,分为Binding和MultiBinding, 如下面分别实现了Combobox的SelectedValuePropperty的Binding
和TextBox的TextProperty的MultiBinding。其中都有ValidationRule。
<ComboBox x:Name="cmbAgeType" Margin="3"
SelectionChanged="cmbAgeType_SelectionChanged" Background="#00000000" BorderBrush="Black" Grid.Row="4" MinWidth="0" Grid.Column="2" IsTabStop="False" SelectedIndex="0" d:LayoutOverrides="GridBox" Tag="PatientAge"
Visibility="{Binding DataContext, ElementName=window, Converter={StaticResource KeyToVisibilityConverter}, ConverterParameter=PatientAge}">
<ComboBox.SelectedValue>
<Binding Path="PatientAge" Converter="{StaticResource AgeMeasureConverter}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<McsfPAFEContainee_ValidationRules:EmptyValidationRule ValidatesOnTargetUpdated="True" ValidationStep="ConvertedProposedValue"/>
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedValue>
<!--<Binding Path="PatientAge" Converter="" UpdateSourceTrigger="PropertyChanged"/>-->
</ComboBox>
<TextBox x:Name="txtPatientWeight" TextWrapping="Wrap" Margin="3" MaxLength="10" TabIndex="6" BorderBrush="Black" Grid.Row="5" Grid.Column="1" Height="22" MinWidth="42" Tag="PatientWeight"
Visibility="{Binding DataContext, ElementName=window, Converter={StaticResource KeyToVisibilityConverter}, ConverterParameter=PatientWeight}">
<TextBox.Text>
<MultiBinding Mode="TwoWay" Converter="{StaticResource WeightConverter}" UpdateSourceTrigger="PropertyChanged">
<MultiBinding.ValidationRules>
<McsfPAFEContainee_ValidationRules:WeightValidationRule ValidatesOnTargetUpdated="True" ValidationStep="ConvertedProposedValue"/>
</MultiBinding.ValidationRules>
<Binding Path="PatientWeight"/>
<Binding Path="IsChecked" ElementName="rdoKg"/>
</MultiBinding>
</TextBox.Text>
<i:Interaction.Behaviors>
<McsfPAFEContainee_Behaviors:NumericTextBoxBehavior MinValue="0" MaxValue="300" />
</i:Interaction.Behaviors>
</TextBox>
2. 后台主动触发ValidationRule的验证。 以下方法根据上面的Binding, 分别去取Binding和MultiBinding, 然后调用UpdateSource。
private bool ValidateInput(object child, PRCfgViewModel vm, bool isUpdateSource, bool isEmergency)
{
BindingExpression be = null;
MultiBindingExpression mbe = null;
if (child is TextBox)
{
be = (child as TextBox).GetBindingExpression(TextBox.TextProperty);
if (null == be)
{
mbe = BindingOperations.GetMultiBindingExpression((child as TextBox), TextBox.TextProperty);
}
}
else if (child is DatePicker)
{
be = (child as DatePicker).GetBindingExpression(DatePicker.TextProperty);
if (null == be)
{
mbe = BindingOperations.GetMultiBindingExpression((child as DatePicker), DatePicker.TextProperty);
}
}
else if (child is ComboBox)
{
be = (child as ComboBox).GetBindingExpression(ComboBox.SelectedValueProperty);
if (null == be)
{
mbe = BindingOperations.GetMultiBindingExpression((child as ComboBox), ComboBox.SelectedValueProperty);
}
}
if (null == be && null == mbe)
{
return false;
}
ValidationRule vr = null;
if (null != be && be.ParentBinding.ValidationRules.Count > 0)
{
vr = be.ParentBinding.ValidationRules[0];
}
else if (null != mbe && mbe.ParentMultiBinding.ValidationRules.Count > 0)
{
vr = mbe.ParentMultiBinding.ValidationRules[0];
}
else
{
return false;
}
string bindingPath = "";
if (null != be)
{
bindingPath = be.ParentBinding.Path.Path;
}
else if (null != mbe)
{
Binding bd = mbe.ParentMultiBinding.Bindings[0] as Binding;
bindingPath = bd.Path.Path;
}
bindingPath = bindingPath.Replace(".", "_");
if (vm.Setting.CfgInfo[bindingPath] != null)
{
(vr as BaseValidationRule).IsActive = !isEmergency;
if ((vr as BaseValidationRule).IsActive)
{
(vr as BaseValidationRule).IsAllowEmpty = !(vm.Setting.CfgInfo[bindingPath].IsKeyword);
}
else
{
if (isUpdateSource)
{
if (null != be)
{
be.UpdateSource();
}
else if (null != mbe)
{
mbe.UpdateSource();
}
}
return true;
}
}
else
{
(vr as BaseValidationRule).IsAllowEmpty = true;
}
if (isUpdateSource)
{
if (null != be)
{
be.UpdateSource();
}
else if (null != mbe)
{
mbe.UpdateSource();
}
}
return true;
}
- WPF后台线程更新UI
0.讲点废话 最近在做一个文件搜索的小软件,当文件多时,界面会出现假死的状况,于是乎想到另外开一个后台线程,更新界面上的ListView,但是却出现我下面的问题. 1.后台线程问题 2年前写过一个软件 ...
- 关于WPF后台触发键盘按键
1.变向响应Tab按键 private void Grid_KeyUp(object sender, KeyEventArgs e) { UIElement e ...
- 一种WPF在后台线程更新UI界面的简便方法
WPF框架规定只有UI线程(主线程)可以更新界面,所有其他后台线程无法直接更新界面.幸好,WPF提供的SynchronizationContext类以及C#的Lambda表达式提供了一种方便的解决方法 ...
- WPF 精修篇 非UI进程后台更新UI进程
原文:WPF 精修篇 非UI进程后台更新UI进程 <Grid> <Grid.RowDefinitions> <RowDefinition Height="11* ...
- WPF 支持的多线程 UI 并不是线程安全的
原文:WPF 支持的多线程 UI 并不是线程安全的 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.欢迎转载.使用.重新发布,但务必保留文章署名吕毅(包含链 ...
- WPF后台动画DoubleAnimation讲解
WPF后台动画,使用DoubleAnimation做的. 1.移动动画 需要参数(目标点离最上边的位置,目标点离最左边的位置,元素名称) Image mImage = new Image(); Flo ...
- How do I duplicate a resource reference in code behind in WPF?如何在WPF后台代码中中复制引用的资源?
原文 https://stackoverflow.com/questions/28240528/how-do-i-duplicate-a-resource-reference-in-code-behi ...
- WPF后台设置xaml控件的样式System.Windows.Style
WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵 http://3w.suchso.com/projecteac-tual/wpf-zhi ...
- 使用WPF来创建 Metro UI程序
本文转载:http://www.cnblogs.com/TianFang/p/3184211.html 这个是我以前网上看到的一篇文章,原文地址是:Building a Metro UI with W ...
随机推荐
- python学习(十)赋值、表达式、if、while、for
明天以搞定这几个应该不难 赋值.表达式.if.while.for 函数.作用域.参数.函数高级话题 迭代和解析一.二 还有我的<30天自制操作系统>没看 #!/usr/bin/python ...
- Chapter 4 马尔科夫链
4.1 引言 现在要研究的是这样一种过程: 表示在时刻的值(或者状态),想对一串连续时刻的值,比如:,, ... 建立一个概率模型. 最简单的模型就是:假设都是独立的随机变量,但是通常这种假设都是没什 ...
- uboot生成随机的MAC地址
转载:http://blog.chinaunix.net/uid-25885064-id-3303132.html 在使用U-boot时,有个问题就是MAC地址的设置,如果MAC地址相同的两块开发板在 ...
- Unity3D研究院之拓展Scene视图
Scene视图是编辑游戏模型的地方,其实它还可以进行编辑.如下图所示,我给Scene视图做了简单的编辑. Scene视图的拓展是基于对象的,意思就是你必须在Hierarchy视图中选择一个对象才行.H ...
- win本地配置docker环境
先上官网链接:https://docs.docker.com/get-started/part2/#introduction 优质入门教程:http://www.docker.org.cn/book/ ...
- hdu4612 无向图中随意加入一条边后使桥的数量最少 / 无向图缩点+求树的直径
题意如上,含有重边(重边的话,俩个点就能够构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选随意起点U,进行bfs,到达最远的一个点v ...
- MySQL mysqlbinlog
MySQL binlog日志记录了MySQL数据库从启用日志以来所有对当前数据库的变更.binlog日志属于二进制文件,我们可以从binlog提取出来生成可读的文本或者SQL语句来重建当前数据库以及根 ...
- 【BZOJ2245】[SDOI2011]工作安排 拆边费用流
[BZOJ2245][SDOI2011]工作安排 Description 你的公司接到了一批订单.订单要求你的公司提供n类产品,产品被编号为1~n,其中第i类产品共需要Ci件.公司共有m名员工,员工被 ...
- 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆
[BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...
- ES通过API调整设置
1.查询es的设置信息 2.查询单个索引的设置 3.设置复制集为0