带有代码的错误模板的文本框
TextBox with Error Template with code
我试图通过代码向表单添加一些文本框,而不是 XAML。并使用错误验证。我有以下代码:
SearchTextBox stb = new SearchTextBox()
{
Name = "stbRelatorio_" + id
};
// Create a new Binding.
Binding bindingStb = new Binding();
bindingStb.Source = model;
bindingStb.Path = new PropertyPath("ReportFile[" + id + "]");
stb.SetBinding(SearchTextBox.FileNameProprety, bindingStb);
BindingExpression bindingExpression =
BindingOperations.GetBindingExpression(stb, SearchTextBox.FileNameProprety);
BindingExpressionBase bindingExpressionBase =
BindingOperations.GetBindingExpressionBase(stb, SearchTextBox.FileNameProprety);
ValidationError validationError =
new ValidationError(new ExceptionValidationRule(), bindingExpression);
Validation.MarkInvalid(bindingExpressionBase, validationError);
ControlTemplate ct = this.Resources["validationErrorTemplate"] as ControlTemplate;
当我这样做时,我得到了没有插入文本时的默认行为(这是我的错误案例)。文本框周围的红色框。我现在想要的是使用自定义的 Adorner 布局,我在 XAML
上构建它
<ControlTemplate x:Name="validationErrorTemplate" x:Key="validationErrorTemplate">
<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<Grid Width="12" Height="12">
<Ellipse Width="12" Height="12"
Fill="Red" HorizontalAlignment="Center"
VerticalAlignment="Center"></Ellipse>
<TextBlock Foreground="White" FontWeight="Heavy"
FontSize="8" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"
ToolTip="{Binding ElementName=ErrorAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
X
</TextBlock>
</Grid>
<TextBlock Foreground="Red" FontWeight="12" Margin="2,0,0,2"
Text="{Binding ElementName=ErrorAdorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
</TextBlock>
</StackPanel>
<AdornedElementPlaceholder x:Name="ErrorAdorner" />
</DockPanel>
</ControlTemplate>
但是当我尝试设置 ErrorTemplate 时:
Validation.SetErrorTemplate(stb, ct);
我没有得到任何错误验证,甚至默认模板也没有。
我是不是漏掉了什么?
SearchTextBox
是从 TextBox
继承来的,我说得对吗?
然后更改对此的绑定:
stb.SetBinding(TextBox.TextProperty, bindingStb);
更新:
ControlTemplate ct = FindResource("validationErrorTemplate") as ControlTemplate;
Validation.SetErrorTemplate(stb, ct);
当你
ControlTemplate ct = this.Resources["validationErrorTemplate"] as ControlTemplate;
Validation.SetErrorTemplate(stb, ct);
而且连默认错误模板都没有,很可能ct
为空
确保 "validationErrorTemplate" 存储在 Window (this
) 资源中。如果模板存储在资源中的某处,可以通过 FindResource
方法
找到
ControlTemplate ct = stb.FindResource("validationErrorTemplate") as ControlTemplate;
Validation.SetErrorTemplate(stb, ct);
我试图通过代码向表单添加一些文本框,而不是 XAML。并使用错误验证。我有以下代码:
SearchTextBox stb = new SearchTextBox()
{
Name = "stbRelatorio_" + id
};
// Create a new Binding.
Binding bindingStb = new Binding();
bindingStb.Source = model;
bindingStb.Path = new PropertyPath("ReportFile[" + id + "]");
stb.SetBinding(SearchTextBox.FileNameProprety, bindingStb);
BindingExpression bindingExpression =
BindingOperations.GetBindingExpression(stb, SearchTextBox.FileNameProprety);
BindingExpressionBase bindingExpressionBase =
BindingOperations.GetBindingExpressionBase(stb, SearchTextBox.FileNameProprety);
ValidationError validationError =
new ValidationError(new ExceptionValidationRule(), bindingExpression);
Validation.MarkInvalid(bindingExpressionBase, validationError);
ControlTemplate ct = this.Resources["validationErrorTemplate"] as ControlTemplate;
当我这样做时,我得到了没有插入文本时的默认行为(这是我的错误案例)。文本框周围的红色框。我现在想要的是使用自定义的 Adorner 布局,我在 XAML
上构建它<ControlTemplate x:Name="validationErrorTemplate" x:Key="validationErrorTemplate">
<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<Grid Width="12" Height="12">
<Ellipse Width="12" Height="12"
Fill="Red" HorizontalAlignment="Center"
VerticalAlignment="Center"></Ellipse>
<TextBlock Foreground="White" FontWeight="Heavy"
FontSize="8" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"
ToolTip="{Binding ElementName=ErrorAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
X
</TextBlock>
</Grid>
<TextBlock Foreground="Red" FontWeight="12" Margin="2,0,0,2"
Text="{Binding ElementName=ErrorAdorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
</TextBlock>
</StackPanel>
<AdornedElementPlaceholder x:Name="ErrorAdorner" />
</DockPanel>
</ControlTemplate>
但是当我尝试设置 ErrorTemplate 时:
Validation.SetErrorTemplate(stb, ct);
我没有得到任何错误验证,甚至默认模板也没有。
我是不是漏掉了什么?
SearchTextBox
是从 TextBox
继承来的,我说得对吗?
然后更改对此的绑定:
stb.SetBinding(TextBox.TextProperty, bindingStb);
更新:
ControlTemplate ct = FindResource("validationErrorTemplate") as ControlTemplate;
Validation.SetErrorTemplate(stb, ct);
当你
ControlTemplate ct = this.Resources["validationErrorTemplate"] as ControlTemplate;
Validation.SetErrorTemplate(stb, ct);
而且连默认错误模板都没有,很可能ct
为空
确保 "validationErrorTemplate" 存储在 Window (this
) 资源中。如果模板存储在资源中的某处,可以通过 FindResource
方法
ControlTemplate ct = stb.FindResource("validationErrorTemplate") as ControlTemplate;
Validation.SetErrorTemplate(stb, ct);