我正在尝试将样式应用于装饰元素,但我不知道正确的语法。这是我尝试过的:
<!-- ValidationRule Based Validitaion Control Template -->
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder Style="textStyleTextBox"/>
</DockPanel>
</ControlTemplate>
唯一的问题是以下行不起作用:
<AdornedElementPlaceholder Style="textStyleTextBox"/>
任何帮助将不胜感激。
Thanks,
-Charles
需要把资源来自哪里。
<TextBox Style="{StaticResource textStyleTextBox}"/>
然后在资源(例如用户控件资源)中定义样式:
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="textStyleTextBox">
<Setter Property="Background" Value="Blue"/>
</Style>
</UserControl.Resources>
但是我不相信您想在占位符中设置装饰元素的样式。它只是该模板的任何控件的占位符。您应该在元素本身中设置装饰元素的样式,就像我上面提供的示例一样。如果您想根据其验证来设置控件的样式,则如下所示:
<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)