简体   繁体   English

Xaml(WPF)中ViewModel的Validation.ErorrTemplate动态更改

[英]Validation.ErorrTemplate dynamic change from viewModel in Xaml (Wpf)

Is there a way to change the Validation.ErrorTemplate from viewModel ,based on property Error Type in mvvm style with IDataErrorInfo. 有没有一种方法可以从ViewModel更改基于IDataErrorInfo的mvvm样式中的属性Error Type的Validation.ErrorTemplate。

Eg. 例如。 I have a property with two different validation templates , in one i display in a way and the another in another way based on the property if required show a choosed template, if exceeds choosed the second one. 我有一个带有两个不同验证模板的属性,一个基于属性显示,另一个基于属性显示,如果需要,则显示选择的模板,如果超过则选择第二个。

Thanks in advance Ben 预先感谢本

use this in your resource 在您的资源中使用它

<ControlTemplate x:Key="ct1">
    <!--Your First Template-->
</ControlTemplate>
<ControlTemplate x:Key="ct2">
    <!--Your Second Template-->
</ControlTemplate>

<Style TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding YourProperty}" Value="yourValue1">
            <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ct1}"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding YourProperty}" Value="yourValue2">
            <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ct2}"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM