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 ...
随机推荐
- php html_entity_decode使用总结
在处理网页字符串的时候,尤其是做爬虫类的应用时,经常会涉及到要处理的字符串中包含html标签,现在对这类字符串的处理做一个小的总结: 有时候获取到的字符串中有html标签,在入库的时候出于安全的考虑通 ...
- ZIP解压缩文件的工具类【支持多级文件夹|全】
ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...
- Jenkins--Run shell command in jenkins as root user?
You need to modify the permission for jenkins user so that you can run the shell commands. You can i ...
- Mac Security工具使用总结find-identity
Security是Mac系统中钥匙串和安全模块的命令行管理工具,(图形化工具为Keychain Access.app).钥匙串(Keychain)实质上就是一个用于存放证书.密钥.密码等安全认证实体的 ...
- 消息队列Handler的用法
下面是每隔一段时间就执行某个操作,直到关闭定时操作: final Handler handler = new Handler(); Runnable runnable = new Runnable() ...
- Java泛型【转】
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: public class GenericTest { public static void main(String[] a ...
- 【BZOJ2427】[HAOI2010]软件安装 Tarjan+树形背包
[BZOJ2427][HAOI2010]软件安装 Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为 ...
- 2017-2018-1 20179209《Linux内核原理与分析》第六周作业
一.分析system_call中断处理过程 实验 下载最新menu,并在test.c中增加mkdir与mkdir-asm函数原型 rm menu -rf git clone https://githu ...
- 我的Java开发学习之旅------>Java经典排序算法之二分插入排序
一.折半插入排序(二分插入排序) 将直接插入排序中寻找A[i]的插入位置的方法改为采用折半比较,即可得到折半插入排序算法.在处理A[i]时,A[0]--A[i-1]已经按关键码值排好序.所谓折半比较, ...
- Pentaho BIServer Community Edtion 6.1 使用教程 第一篇 软件安装
一.简介: Pentaho BI Server 分为企业版和社区版两个版本.其中 社区版 CE(community edtion) 为免费版本. 二.下载CE版(CentOS): 后台下载命令: no ...