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 に答える