Link to home
Start Free TrialLog in
Avatar of Kevin Robinson
Kevin Robinson

asked on

WPF DataTrigger

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>
Avatar of jonnidip
jonnidip
Flag of Italy image

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.
Avatar of Kevin Robinson
Kevin Robinson

ASKER

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}"/>
ASKER CERTIFIED SOLUTION
Avatar of jonnidip
jonnidip
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok let me try
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>
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>