2

角が丸い境界線で囲まれたテキストボックスがあります。どちらも背景色が同じで、1 つのデータ入力ボックスとして表示されます。ユーザーがテキストボックスをクリックすると、テキストボックスの背景と境界線の色が変わります。スタイリングが MainWindow にあるときに、これが機能しています。ただし、MainWindow の XAML からすべてのスタイリングを中央のリソース ディクショナリに抽象化しようとしています。そうすることで、ElementNameがスコープ内になくなったため、境界線の背景色を変更するDataTriggerが機能していないことがわかりました(少なくともそれが問題だと思います)。テストプロジェクト/ソリューションでこれを行うことで物事を簡素化しようとしましたが、データトリガーを機能させる方法を見つけることができないようです. XAML ファイルが 2 つあるだけです。1 つは MainWindow で、もう 1 つはリソース ディクショナリです。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ResourceDictionary Source="/MainSkins.xaml"/>
    </Window.Resources>

    <Grid HorizontalAlignment="Left" Width="307" Margin="83,0,0,0">
        <Border Style="{StaticResource AnimatedInputTextBoxBorder}"      
                Margin="10,76,10,151">
            <TextBox Name="txtTransitRoutingNumber" Style="{StaticResource 
                     AnimatedInputTextBox}" 
                     HorizontalAlignment="Left" Height="73" Margin="9,9,0,0" 
                     TextWrapping="Wrap" Text="" 
                     VerticalAlignment="Top" 
                     Width="267"/>
        </Border>
    </Grid>
</Window>

上記のように、 MainWindow.xaml とはまったく異なるファイルにあるリソース ディクショナリは次のとおりです。

<ResourceDictionary 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApplication1">

    <Style x:Key="AnimatedInputTextBoxBorder" TargetType="{x:Type Border}">
        <Setter Property="Background" Value="#DADADA"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="CornerRadius" Value="15"/>
        <Setter Property="BorderBrush" Value="#DADADA"/>
        <Style.Triggers>

            <!--THIS DATA TRIGGER IS NOT WORKING-->
            <DataTrigger Binding="{Binding Path=IsFocused}" Value="true">
                <Setter Property="Background" Value="#C2E4F6" />
                <Setter Property="BorderBrush" Value="#C2E4F6"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="AnimatedInputTextBox" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="#DADADA"/>
        <Setter Property="Foreground" Value="#000000" />
        <Setter Property="BorderThickness" Value="0"/>
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Background" Value="#C2E4F6"/>
            </Trigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>

XAMLに関してはまったくの初心者なので、どんな助けも大歓迎です。

4

1 に答える 1

2

Chris W. のおかげで、うまく機能する回避策を見つけることができました。解決策は次のとおりです。

<!--This style defines the animated input TextBoxes with rounded corners-->
 <Style x:Key="AnimatedTextBoxStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Background" Value="#DADADA" />
    <Setter Property="BorderBrush" Value="#DADADA"  />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="TextAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
       <Setter.Value>
           <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"  
                        BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
                       <ScrollViewer x:Name="PART_ContentHost" />
                </Border>
           <ControlTemplate.Triggers>
               <Trigger Property="IsFocused" Value="True">
                   <Setter Property="Foreground" Value="Black" />
                   <Setter Property="Background" Value="#C2E4F6" />
               </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
      </Setter.Value>
    </Setter>
 </Style>
于 2016-03-22T16:50:53.557 に答える