Cha*_*les 4 validation wpf styles
我正在尝试将样式应用于装饰元素,但我不知道正确的语法.这是我尝试过的:
<!-- ValidationRule Based Validitaion Control Template -->
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder Style="textStyleTextBox"/>
</DockPanel>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
唯一的问题是以下行不起作用:
<AdornedElementPlaceholder Style="textStyleTextBox"/>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
谢谢,
- 查尔斯
需要放置资源来源.
<TextBox Style="{StaticResource textStyleTextBox}"/>
Run Code Online (Sandbox Code Playgroud)
然后在资源中定义样式,例如用户控制资源:
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="textStyleTextBox">
<Setter Property="Background" Value="Blue"/>
</Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
但是我不相信你想在占位符中设置adornedelement的样式.它只是该模板的任何控件的占位符.您应该在元素本身中设置adornedelement的样式,就像我上面提供的示例一样.如果你想根据它的验证来设置控件的样式,那么像这样:
<Window.Resources>
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Yellow" Width="55" FontSize="18">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel x:Name="mainPanel">
<TextBlock>Age:</TextBlock>
<TextBox x:Name="txtAge"
Validation.ErrorTemplate="{DynamicResource validationTemplate}"
Style="{StaticResource textBoxInError}">
<Binding Path="Age" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9710 次 |
| 最近记录: |