如何从单独的XAML资源字典(不同的文件)设置DataTrigger上的ElementName和Path

时间:2016-03-21 19:15:42

标签: wpf xaml resourcedictionary datatrigger elementname

我有一个被圆角边框包围的文本框。它们都具有相同的背景颜色,使它们显示为单个数据输入框。当用户单击文本框时,文本框的背景和边框会更改颜色。样式在我的MainWindow中时,我有这个工作。但是,我试图将我的所有样式从MainWindow中的XAML抽象为中央资源字典。这样做,我发现改变边框背景颜色的DataTrigger不起作用,因为ElementName不再在范围内(至少我认为这是问题)。我试图通过在测试项目/解决方案中执行此操作来简化操作,但似乎无法找到使数据触发器工作的方法。我只有两个XAML文件。一个是我的MainWindow,另一个是我的资源词典。 MainWindow XAML如下:

<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方面我是一个全新的人。

1 个答案:

答案 0 :(得分:1)

感谢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>