wpf中有validateRule类, 用于界面元素的验证, 如何后台去控制validateRule呢?
1. UI层要binding写好的ValidateRule,分为Binding和MultiBinding, 如下面分别实现了Combobox的SelectedValuePropperty的Binding
和TextBox的TextProperty的MultiBinding。其中都有ValidationRule。
<ComboBoxx: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="{BindingDataContext,ElementName=window,Converter={StaticResourceKeyToVisibilityConverter},ConverterParameter=PatientAge}">
<ComboBox.SelectedValue>
<BindingPath="PatientAge"Converter="{StaticResourceAgeMeasureConverter}"Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<McsfPAFEContainee_ValidationRules:EmptyValidationRuleValidatesOnTargetUpdated="True"ValidationStep="ConvertedProposedValue"/>
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedValue>
<!--<Binding Path="PatientAge" Converter="" UpdateSourceTrigger="PropertyChanged"/>-->
</ComboBox>
<TextBoxx: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="{BindingDataContext,ElementName=window,Converter={StaticResourceKeyToVisibilityConverter},ConverterParameter=PatientWeight}">
<TextBox.Text>
<MultiBinding Mode="TwoWay"Converter="{StaticResourceWeightConverter}" UpdateSourceTrigger="PropertyChanged">
<MultiBinding.ValidationRules>
<McsfPAFEContainee_ValidationRules:WeightValidationRuleValidatesOnTargetUpdated="True"ValidationStep="ConvertedProposedValue"/>
</MultiBinding.ValidationRules>
<BindingPath="PatientWeight"/>
<BindingPath="IsChecked"ElementName="rdoKg"/>
</MultiBinding>
</TextBox.Text>
<i:Interaction.Behaviors>
<McsfPAFEContainee_Behaviors:NumericTextBoxBehaviorMinValue="0"MaxValue="300" />
</i:Interaction.Behaviors>
</TextBox>
2. 后台主动触发ValidationRule的验证。 以下方法根据上面的Binding, 分别去取Binding和MultiBinding, 然后调用UpdateSource。
privateboolValidateInput(objectchild,PRCfgViewModelvm,boolisUpdateSource,boolisEmergency)
{
BindingExpressionbe =null;
MultiBindingExpressionmbe =null;
if(childisTextBox)
{
be = (childasTextBox).GetBindingExpression(TextBox.TextProperty);
if(null== be)
{
mbe =BindingOperations.GetMultiBindingExpression((childasTextBox),TextBox.TextProperty);
}
}
elseif(childisDatePicker)
{
be = (childasDatePicker).GetBindingExpression(DatePicker.TextProperty);
if(null== be)
{
mbe =BindingOperations.GetMultiBindingExpression((childasDatePicker),DatePicker.TextProperty);
}
}
elseif(childisComboBox)
{
be = (childasComboBox).GetBindingExpression(ComboBox.SelectedValueProperty);
if(null== be)
{
mbe =BindingOperations.GetMultiBindingExpression((childasComboBox),ComboBox.SelectedValueProperty);
}
}
if(null== be &&null== mbe)
{
returnfalse;
}
ValidationRulevr =null;
if(null!= be && be.ParentBinding.ValidationRules.Count > 0)
{
vr = be.ParentBinding.ValidationRules[0];
}
elseif(null!= mbe && mbe.ParentMultiBinding.ValidationRules.Count > 0)
{
vr = mbe.ParentMultiBinding.ValidationRules[0];
}
else
{
returnfalse;
}
stringbindingPath ="";
if(null!= be)
{
bindingPath = be.ParentBinding.Path.Path;
}
elseif(null!= mbe)
{
Bindingbd = mbe.ParentMultiBinding.Bindings[0]asBinding;
bindingPath = bd.Path.Path;
}
bindingPath = bindingPath.Replace(".","_");
if(vm.Setting.CfgInfo[bindingPath] !=null)
{
(vrasBaseValidationRule).IsActive = !isEmergency;
if((vrasBaseValidationRule).IsActive)
{
(vrasBaseValidationRule).IsAllowEmpty = !(vm.Setting.CfgInfo[bindingPath].IsKeyword);
}
else
{
if(isUpdateSource)
{
if(null!= be)
{
be.UpdateSource();
}
elseif(null!= mbe)
{
mbe.UpdateSource();
}
}
returntrue;
}
}
else
{
(vrasBaseValidationRule).IsAllowEmpty =true;
}
if(isUpdateSource)
{
if(null!= be)
{
be.UpdateSource();
}
elseif(null!= mbe)
{
mbe.UpdateSource();
}
}
returntrue;
}
- 前言
- win32与WPF的混合编程
- WPF: 一个可以用StoryBoard动态改变Grid行宽/列高的类
- MFC中调用WPF教程
- Expression Blend操作: 使用behavior来控制Storyboard
- WPF DatePicker 的textbox的焦点
- WPF 使用MultiBinding ,TwoWay ,ValidationRule ,需要注意的事项
- WPF TreeView 后台C#选中指定的Item, 需要遍历
- WPF GridViewColumn Sort DataTemplate
- DataGridColum的bug
- WPF Get Multibinding Expression, Update Source,
- WPF 后台触发 Validate UI‘s Element
- WPF ValidationRule 触发ErrorTemplate 的注意事项
- WPF DelegateCommand CanExecute
- WPF TextBox PreviewTextInput handle IME (chinese)
- No overload for &#39;OnStartup&#39; matches delegate &#39;System.Windows.StartupEventHandler&#39;
- WPF error: does not contain a static &#39;Main&#39; method suitable for an entry point
- WPF GridView中的CellTemplate失效的原因
- DataGrid 显示选中的item
- 如何得到WPF中控件绑定的EventTrigger
- 选中DataGrid的Cell而不是row
- ContextMenu的自定义
- 输入框只能输入英文
- TextBox的OnTextboxChanged事件里对Text重新赋值带中文, 导致崩溃
- DataGrid当列宽超出当前宽度时,没有数据也恒有滚动条
- wpf如何获取control template里的元素
- Set connectionId threw an exception.
- WPF中Visible设为Collapse时,VisualTreeHelper.GetChildrenCount为0
- XAML 编码规范 (思考)
- 如何为现有控件的DependencyProperty添加Value Changed事件?
- TreeView滚动TreeViewItem
- 为BindingList添加Sort
- WPF Background的设置有坑
- 自定义Panel中添加依赖属性需要注意的问题
- TextBlock截断字符显示为....
- DataGrid 支持字符截断显示
- TreeView控件实践
- WPF如何更改系统控件的默认高亮颜色 (Highlight brush)
- ViewModel中C# Property自动添加OnPropertyChanged处理的小工具, 以及相应Python知识点
- WPF中Xaml编译正常而Designer Time时出错的解决办法
- 关于Snoop的用法
- wpf中为DataGrid添加checkbox支持多选全选
- WPF中DataGrid控件的过滤(Filter)性能分析及优化
- wpf控件提示Value ‘’ can not convert
- DropShadowEffect导致下拉框控件抖动
- 再论WPF中的UseLayoutRounding和SnapsToDevicePixels
- WPF案例:如何设计历史记录查看UI
- WPF案例:如何设计搜索框(自定义控件的原则和方法)
- WPF基本概念入门
- WPF开发中Designer和码农之间的合作
- 聊聊WPF中的Dispatcher
- 聊聊WPF中字体的设置
- Bug:DataGridCell的显示不完整
- WPF中ToolTip的自定义
- WPF中ItemsControl绑定到Google ProtocolBuffer的结构体时的性能问题
- TreeView的性能问题
- Xaml中string(字符串)常量的定义以及空格的处理
- 依赖属性
- WPF中的CheckBox的_ (underscore / 下划线)丢失
- WPF错误:必须使“Property”具有非 null 值。
- WPF中ItemsControl应用虚拟化时找到子元素的方法
- WPF毫秒级桌面时钟的实现-C#中Hook(钩子)的应用
- KB2464222导致IsNonIdempotentProperty方法找不见
- WPF中PreviewMouseDownEvent的系统处理:TabItem的PreviewMouseDown 事件弹框后不切换的问题调查
- WPF文字渲染相关的问题及解决
- wpf中的默认右键菜单中的复制、粘贴、剪贴等没有本地化的解决方案
- WPF内部DeliverEvent读锁和PrivateAddListener写锁导致死锁
- Windbg调试WPF的依赖属性
- WPF 后台Render线程崩溃, Exception from HRESULT: 0x88980406
- WPF中DependencyObject与DependencyProperty的源码简单剖析
- 禁用WPF中DataGrid默认的鼠标左键拖动多选行的效果
- wpf工程中在Xaml文件下添加多个cs文件
- ScrollViewer滚动到底来触发加载数据的Behavior