mike allen
mike allen

Reputation: 45

How to set ElementName and Path on a DataTrigger from a separate XAML resource dictionary (a different file)

I have a textbox surrounded by a border with rounded corners. They both have the same background color to make them appear as a single data entry box. When the user clicks in the textbox, the background of the textbox and the border change color. I have this working when the styling is in my MainWindow. However, I am trying to abstract all of my styling from the XAML in MainWindow to a central resource dictionary. In doing so, I find that the DataTrigger which changes the background color of the border isn't working, due to the fact that ElementName is no longer in scope (at least I think that is the issue). I have tried to simplify things by doing this in a test project/solution but can't seem to find a way to get the data trigger to work. I simply have two XAML files. One is my MainWindow, the other is my resource dictionary. The MainWindow XAML is as follows:

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

And here is my Resource Dictionary which as I mentions above is in a completely different file from 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>

Any help would be very much appreciated, as I am a total newbie when it comes to XAML.

Upvotes: 2

Views: 1321

Answers (1)

mike allen
mike allen

Reputation: 45

Thanks to Chris W., I was able to find a workaround that works great. Here is the solution:

<!--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>

Upvotes: 2

Related Questions