Solved

WPF DataTrigger

Posted on 2013-06-27
6
786 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

15 Experts available now in Live!

Get 1:1 Help Now