[英]Access Validation.HasError from UserControl
could someone help me, how can I access the HasError property from my custom usercontrol? 有人可以帮助我,如何从自定义用户控件访问HasError属性?
<UserControl.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0,2,0,2" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<!--<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white">
</TextBlock>-->
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="Layout" DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox Name="TbHostname" Grid.Column="1"
VerticalContentAlignment="Center" MinWidth="200">
<TextBox.Text>
<Binding Path="Hostname" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" NotifyOnValidationError="True">
<Binding.ValidationRules>
<serverStarter2:HostnameValidator ErrorMessage="Wrong hostname format."/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
This is my custom usercontrol, that is used in MainWindow.xaml . 这是我的自定义用户控件,用于MainWindow.xaml中 。 What I want, when everything is fine, enable submit button, when not, disable it. 我想要的是,如果一切正常,请启用提交按钮,否则请禁用它。
Here is code for button inside MainWindow.xaml : 这是MainWindow.xaml中按钮的代码:
<Button Content="Test connection" Command="{Binding ConnectionViewModel.TestConnectionCommand}"
>
<Button.Style>
<Style TargetType="{x:Type Button}" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=(Validation.HasError), ElementName=StudioHostname}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
ElementName=StudioHostname is name of my UserControl. ElementName = StudioHostname是我的UserControl的名称。
It is the Validation.HasError
attached property of the TextBox
in the UserControl
that is being set to true
. 是设置为true
的UserControl
中TextBox
的Validation.HasError
附加属性。 The Validation.HasError
property of the UserControl
itself won't be set and you can't bind to an element that is defined the UserControl
from the MainWindow
using an ElementName
. 不会设置UserControl
本身的Validation.HasError
属性,并且不能绑定到使用ElementName
从MainWindow
定义为UserControl
的元素。
You are doing this wrong. 您做错了。 What you should do is to implement the validation in the view model class where the Hostname
property is defined. 您应该做的是在定义了Hostname
属性的视图模型类中实现验证。 You can do this by implementing the INotifyDataErrorInfo interface. 您可以通过实现INotifyDataErrorInfo接口来实现。
Then you can simply bind the IsEnabled
property of the Button
to the HasErrors
property of the view model, or bind it to an ICommand
whose CanExecute
method returns the value of the HasErrors
property. 然后,您可以简单地将Button
的IsEnabled
属性绑定到视图模型的HasErrors
属性,或将其绑定到CanExecute
,该ICommand
的CanExecute
方法返回HasErrors
属性的值。
There is an example of how to implement the interface available here: https://social.technet.microsoft.com/wiki/contents/articles/19490.wpf-4-5-validating-data-in-using-the-inotifydataerrorinfo-interface.aspx . 这里有一个有关如何实现接口的示例: https : //social.technet.microsoft.com/wiki/contents/articles/19490.wpf-4-5-validating-data-in-using-the-inotifydataerrorinfo -interface.aspx 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.