C# 如何将验证传递给用户控件的子控件
我创建了一个用户控件,它由一个Telerik RadMaskedInput和一个标签组成。用户控件是一个浮动标签输入,如下所示: 我遇到的问题是,当控件验证时,整个控件都会得到红色的验证边界。我只需要RadMaskedInput框的边框是红色的。我已经看到了其他几篇关于同一问题的帖子和帮助请求,但我似乎找不到任何适合我的东西。我的控件的XAML如下所示:C# 如何将验证传递给用户控件的子控件,c#,wpf,validation,xaml,mvvm,C#,Wpf,Validation,Xaml,Mvvm,我创建了一个用户控件,它由一个Telerik RadMaskedInput和一个标签组成。用户控件是一个浮动标签输入,如下所示: 我遇到的问题是,当控件验证时,整个控件都会得到红色的验证边界。我只需要RadMaskedInput框的边框是红色的。我已经看到了其他几篇关于同一问题的帖子和帮助请求,但我似乎找不到任何适合我的东西。我的控件的XAML如下所示: <Grid> <Label x:Name="controlLabel" Ve
<Grid>
<Label x:Name="controlLabel"
VerticalAlignment="Bottom"
Padding="5,0,5,0"
HorizontalAlignment="Left"
Background="White"
Grid.Row="0"
Margin="10,0,0,0"
Panel.ZIndex="1"
IsHitTestVisible="False"
Template="{DynamicResource LabelControlTemplate1}"
Content="{Binding ElementName=floatingLabelTextBox, Path=LabelContent}"
FontFamily="{Binding ElementName=floatingLabelTextBox, Path=LabelFontFamily}"
FontSize="{Binding ElementName=floatingLabelTextBox, Path=LabelFontSize}"
Foreground="{DynamicResource FloatingLabelTextBox.Label.Foreground}"/>
<telerik:RadMaskedTextInput x:Name="controlMaskedTextBox"
Mask=""
TextMode="PlainText"
FontWeight="Normal"
BorderBrush="{DynamicResource FloatingLabelTextBox.BorderBrush}"
BorderThickness="1"
SelectionOnFocus="SelectAll"
IsClearButtonVisible="False"
VerticalAlignment="Bottom"
HorizontalAlignment="Left"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Center"
Value="{Binding TextBoxText,
Mode=TwoWay,
UpdateSourceTrigger=LostFocus,
ValidatesOnDataErrors=True,
ValidatesOnExceptions=True,
NotifyOnValidationError=True}"
GotFocus="TextBox_GotFocus"
LostFocus="TextBox_LostFocus"
Template="{DynamicResource RadMaskedTextInputControlTemplate1}"/>
</Grid>
最后,我调用/使用控件的代码:
<controls:FloatingLabelTextBox LabelContent="Batch #"
TextBoxText="{Binding BatchNumber.StringValue,
Mode=TwoWay,
UpdateSourceTrigger=LostFocus,
ValidatesOnDataErrors=True,
ValidatesOnExceptions=True,
NotifyOnValidationError=True,
Converter={StaticResource stringToInt}}"
Validation.ErrorTemplate="{x:Null}"
LabelForeground="{DynamicResource FloatingLabelTextBox.Label.Foreground}"
LabelFontSize="12"
TextBoxWidth="150"
TextBoxHeight="30"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Grid.Row="1"
IsEnabled="{Binding BatchNumber.IsReadOnly, Mode=OneWay, Converter={StaticResource inverseBool}}" />
请注意,我使用的是MVVM模式。我已经从这里对代码进行了建模:但没有看到预期的结果。我根本看不到任何验证边界
我在这个问题上讨论得太久了。任何帮助/建议都将不胜感激。我没有得到解决这个问题的好答案。我想尽一切办法。我相信这是可能的,但有一个最后期限,我需要做点什么。我的解决方案是重做控件,以便扩展基本控件。在本例中,我扩展了Telerik Radmaskedtdemiput控件并重写了控件模板,向模板添加了浮动标签,并使用了一系列触发器来实现所需的功能。这允许我对Telerik控件使用本机验证,因此不需要传递任何内容。就我而言,这可能是一个比创建用户控件更好的解决方案。如果需要一个更复杂的控制,我对如何实现这一点没有什么好的建议,我希望看到一些评论或建议来满足我的好奇心。感谢MethodMan花时间回复。你看过Telerik网站了吗?他们有很好的记录示例。我有MethodMan。我认为这超出了Telerik的范围。我的意思是,我已经用一个简单的文本框取代了Telerik控件,并且有同样的问题。换句话说,我想要验证的Telerik控件的孩子有点没有意义。不过谢谢你的建议。为了确保Telerik控件模板中没有任何内容干扰边框颜色的更改,我在另一个IsEnabled=false触发器中将边框颜色更改为红色,它完全更改了。只是没有随着验证错误而改变。
public String TextBoxText
{
get
{
return (String)GetValue(TextBoxTextProperty);
}
set
{
SetValue(TextBoxTextProperty, value);
}
}
// Using a DependencyProperty as the backing store for TextBoxText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBoxTextProperty =
DependencyProperty.Register("TextBoxText", typeof(String), typeof(FloatingLabelTextBox), new PropertyMetadata() { PropertyChangedCallback = UpdateTextBoxText });
private static void UpdateTextBoxText(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FloatingLabelTextBox control = d as FloatingLabelTextBox;
if (control != null && e.NewValue != null)
{
if (!string.IsNullOrWhiteSpace(e.NewValue as string))
{
control.FloatLabel();
}
else
{
control.DockLabel();
}
}
}
<controls:FloatingLabelTextBox LabelContent="Batch #"
TextBoxText="{Binding BatchNumber.StringValue,
Mode=TwoWay,
UpdateSourceTrigger=LostFocus,
ValidatesOnDataErrors=True,
ValidatesOnExceptions=True,
NotifyOnValidationError=True,
Converter={StaticResource stringToInt}}"
Validation.ErrorTemplate="{x:Null}"
LabelForeground="{DynamicResource FloatingLabelTextBox.Label.Foreground}"
LabelFontSize="12"
TextBoxWidth="150"
TextBoxHeight="30"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Grid.Row="1"
IsEnabled="{Binding BatchNumber.IsReadOnly, Mode=OneWay, Converter={StaticResource inverseBool}}" />