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 ...
随机推荐
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- 写一段代码,判断一个包括'{','[','(',')',']','}'的表达式是否合法(注意看样例的合法规则。) 给定一个表达式A,请返回一个bool值,代表它是否合法。
这道题比较奇怪,它的匹配规则并不是我们平时想想的那种匹配规则,例如:平时的匹配规则是().{}.[]才能匹配,本题中(和} .].)都能匹配.所以做题时要好好审题.另外,本题中给的测试用例是错误的. ...
- C市现在要转移一批罪犯到D市,C市有n名罪犯,按照入狱时间有顺序,另外每个罪犯有一个罪行值,值越大罪越重。现在为了方便管理,市长决定转移入狱时间连续的c名犯人,同时要求转移犯人的罪行值之和不超过t,问有多少种选择的方式?
// ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // ConsoleApplication1 ...
- ndk javah配置
Location: C:\Program Files\Java\jdk1.6.0_25\bin\javah.exe Working Directory: ${project_loc} Argument ...
- C# Array类的浅复制Clone()与Copy()的差别
1 Array.Clone方法 命名空间:System 程序集:mscorlib 语法: public Object Clone() Array的浅表副本仅复制Array的元素,不管他们是引用类型还是 ...
- [原]js获取dom元素的实际位置及相对坐标
关键API: Element.getBoundingClientRect() mdn:https://developer.mozilla.org/en-US/docs/Web/API/Element/ ...
- rtsp转rtmp、hls网页直播服务器EasyNVR前端兼容性调试:ie下的 pointer-events- none
发现问题: 之前在做EasyNVR 的web页面开发过程中,力求的都是一个播放效果的.功能的展示.对于兼容性也有注意,但有些细节还是难免有所疏忽. 内部测试发现:由于我们是流媒体的实时视频直播,在we ...
- Java 8 default 函数
我们知道在java8之前 ,一个类实现一个接口需要实现接口所有的方法, 但是这样会导致一个问题,当一个接口有很多的实现类的时候,修改这个接口就变成了一个非常麻烦的事,需要修改这个接口的所有实现类 不过 ...
- 九度OJ 1030:毕业bg (01背包、DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1814 解决:798 题目描述: 每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为"bg" ...
- mac上完整卸载删除.简单粗暴无脑:androidstudio删除方案
如果你是mac ,你删除as ,删不干净也正常,你会发现安装的时候,前面的东西也在.配置文件在,会导致你以前的错误不想要的东西都在. 废话不多说,复制粘贴就是干!!!!~~~~~~~~ 第一步: 复 ...