5

我正在尝试将样式应用于装饰元素,但我不知道正确的语法。这是我尝试过的:

    <!-- ValidationRule Based Validitaion Control Template -->
    <ControlTemplate x:Key="validationTemplate">
        <DockPanel>
            <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
            <AdornedElementPlaceholder Style="textStyleTextBox"/>
        </DockPanel>
    </ControlTemplate>

唯一的问题是以下行不起作用:

            <AdornedElementPlaceholder Style="textStyleTextBox"/>

任何帮助将不胜感激。

谢谢,

-查尔斯

4

1 回答 1

9

需要把资源的来源放在哪里。

<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>
于 2009-04-01T10:32:13.667 回答