Solved

WPF DataTrigger

Posted on 2013-06-27
6
795 Views
Last Modified: 2013-07-04
I have lots of  text blocks on a form and I want them to change color depending on whether they are postive or negative.  I have the XAML below which does work but I want to reuse this style from a resource dictionary.   How do I do this?

  <TextBlock  Text="{Binding Path=MondayVariance}" Visibility="{Binding Path=MondayVarianceVisible, Converter={StaticResource VisibilityConverter}}"   Grid.Row="2" Grid.Column="3"  VerticalAlignment="Center"  HorizontalAlignment="Center" >
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding MondayVariance, Converter={StaticResource converter}}"
                                     Value="1">
                            <Setter Property="Foreground"  Value="Black"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding MondayVariance, Converter={StaticResource converter}}"
                                     Value="0">
                            <Setter Property="Foreground" Value="Green"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding MondayVariance, Converter={StaticResource converter}}"
                                     Value="-1">
                            <Setter Property="Foreground" Value="Black"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
0
Comment
Question by:Kevin Robinson
  • 4
  • 2
6 Comments
 
LVL 13

Expert Comment

by:jonnidip
ID: 39286298
Just put your style in a dictionary and add a x:key.
<Window.Resources>
        <ResourceDictionary x:Name="resourceDictionary">
			<Style x:Key="myStyle" TargetType="TextBlock">
				<Style.Triggers>
					<DataTrigger Binding="{Binding MondayVariance, Converter={StaticResource converter}}"
								 Value="1">
						<Setter Property="Foreground"  Value="Black"/>
					</DataTrigger>
					<DataTrigger Binding="{Binding MondayVariance, Converter={StaticResource converter}}"
								 Value="0">
						<Setter Property="Foreground" Value="Green"/>
					</DataTrigger>
					<DataTrigger Binding="{Binding MondayVariance, Converter={StaticResource converter}}"
								 Value="-1">
						<Setter Property="Foreground" Value="Black"/>
					</DataTrigger>
				</Style.Triggers>
			</Style>
		</ResourceDictionary>
</Window.Resources>

[...]


<TextBlock  Text="{Binding Path=MondayVariance}" Visibility="{Binding Path=MondayVarianceVisible, Converter={StaticResource VisibilityConverter}}" 
			Grid.Row="2" Grid.Column="3"  VerticalAlignment="Center"  HorizontalAlignment="Center" Style="{StaticResource myStyle}"/>

Open in new window


Regards.
0
 
LVL 3

Author Comment

by:Kevin Robinson
ID: 39291469
Yes but this is Still referencing "MondayVariance"   so it is not resuable.     I want to be able to the same style with the triggers on different textboxes.

<TextBlock  Text="{Binding Path=MondayVariance}" Style="{StaticResource myStyle}"/>
<TextBlock  Text="{Binding Path=TuesdayVariance}" Style="{StaticResource myStyle}"/>
<TextBlock  Text="{Binding Path=WednesdayVariance}" Style="{StaticResource myStyle}"/>
0
 
LVL 13

Accepted Solution

by:
jonnidip earned 500 total points
ID: 39292302
You may try to bind directly to the Text property of your textblock.
It should look something like this:
<Style x:Key="myStyle" TargetType="TextBlock">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource converter}}" Value="1">
            <Setter Property="Foreground"  Value="Black"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource converter}}" Value="0">
            <Setter Property="Foreground" Value="Green"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource converter}}" Value="-1">
            <Setter Property="Foreground" Value="Black"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

Open in new window


I tested with a test page and it seems to work as expected.

Regards.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 3

Author Comment

by:Kevin Robinson
ID: 39299513
Ok let me try
0
 
LVL 3

Author Comment

by:Kevin Robinson
ID: 39299542
I have this but dosent work.


<UserControl.Resources>

<Style x:Key="myStyle" TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource VisibilityConverter}}"
                                     Value="1">
                    <Setter Property="Foreground" Value="Black"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource VisibilityConverter}}"
                                     Value="-1">
                    <Setter Property="Foreground" Value="Green"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self},  Converter={StaticResource VisibilityConverter}}"
                                     Value="0">
                    <Setter Property="Background" Value="DarkGreen"/>
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="FontSize" Value="11"/>
                    <Setter Property="Padding" Value="2,0,2,0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>


 <TextBlock  Text="{Binding Path=Wednesday.Variance}"  HorizontalAlignment="Right"
                                Style="{StaticResource myStyle}">
                    </TextBlock>
0
 
LVL 3

Author Comment

by:Kevin Robinson
ID: 39299582
My Mistake works fine


<UserControl.Resources>

<Style x:Key="myStyle" TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource Converter}}"
                                     Value="1">
                    <Setter Property="Foreground" Value="Black"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource Converter}}"
                                     Value="-1">
                    <Setter Property="Foreground" Value="Green"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource Self},  Converter={StaticResource Converter}}"
                                     Value="0">
                    <Setter Property="Background" Value="DarkGreen"/>
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="FontSize" Value="11"/>
                    <Setter Property="Padding" Value="2,0,2,0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>


 <TextBlock  Text="{Binding Path=Wednesday.Variance}"  HorizontalAlignment="Right"
                                Style="{StaticResource myStyle}">
                    </TextBlock>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
fso.FolderExists("\\server\HiddenFolder$") 4 51
ASP.NET 5 Templates 2 66
Help with LINQ and XML 10 27
Coding C# in Linux 8 31
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now