Solved

WPF DataTrigger

Posted on 2013-06-27
6
845 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
[X]
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
  • 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
Stressed Out?

Watch some penguins on the livecam!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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