Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

WPF DataTrigger

Posted on 2013-06-27
6
809 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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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