locked
Changing DataTemplate's TextBlock Foreground based on DataTrigger RRS feed

  • Question

  • I have a DataTemplate which contains a single TextBlock. I want the Foreground of the TextBlock to change depending on the value of a bound data object. This in my DataTemplate:

     

    Code Snippet

     

    <DataTemplate x:Key="MyTemplate">

    <TextBlock Text="{Binding Path=ConflictName}">

    <TextBlock.Triggers>

    <DataTrigger Binding="{Binding Path=AutoResolve}" Value="False">

    <Setter Property="Foreground" Value="Red" />

    </DataTrigger>

    <DataTrigger Binding="{Binding Path=AutoResolve}" Value="True">

    <Setter Property="Foreground" Value="Black" />

    </DataTrigger>

    </TextBlock.Triggers>

    </TextBlock>

    </DataTemplate>

     

     

    However, I get the following errors:

    Cannot find the static member 'ForegroundProperty' on the type 'ContentPresenter'.

    Value 'Foreground' cannot be assigned to property 'Property'. Invalid PropertyDescriptor value.

     

     

    Any help fixing up this XAML?
    Monday, May 26, 2008 2:41 PM

Answers

  • The following XAMLPad ready example shows how to do this:

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlnsTongue Tiedys="clr-namespaceTongue Tiedystem;assembly=mscorlib">
        <Grid>
            <ListBox Width="200" Height="200" BorderBrush="Navy" BorderThickness="1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}" x:Name="textBlock"/>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=Name}" Value="Button">
                                <Setter Property="Foreground" Value="Red" TargetName="textBlock"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=Name}" Value="ListBox">
                                <Setter Property="Foreground" Value="Green" TargetName="textBlock"/>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemsSource>
                    <x:Array Type="{x:Type sys:Type}">
                        <x:Type TypeName="TextBox"/>
                        <x:Type TypeName="Button"/>
                        <x:Type TypeName="ListBox"/>
                        <x:Type TypeName="ListView"/>
                    </x:Array>
                </ListBox.ItemsSource>
            </ListBox>
        </Grid>
    </Page>

    Hope this helps
    Thursday, May 29, 2008 7:23 AM

All replies

  • The following XAMLPad ready example shows how to do this:

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlnsTongue Tiedys="clr-namespaceTongue Tiedystem;assembly=mscorlib">
        <Grid>
            <ListBox Width="200" Height="200" BorderBrush="Navy" BorderThickness="1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}" x:Name="textBlock"/>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=Name}" Value="Button">
                                <Setter Property="Foreground" Value="Red" TargetName="textBlock"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=Name}" Value="ListBox">
                                <Setter Property="Foreground" Value="Green" TargetName="textBlock"/>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemsSource>
                    <x:Array Type="{x:Type sys:Type}">
                        <x:Type TypeName="TextBox"/>
                        <x:Type TypeName="Button"/>
                        <x:Type TypeName="ListBox"/>
                        <x:Type TypeName="ListView"/>
                    </x:Array>
                </ListBox.ItemsSource>
            </ListBox>
        </Grid>
    </Page>

    Hope this helps
    Thursday, May 29, 2008 7:23 AM
  • Gosh. So simple. I had figured out how to place the triggers in a Style, but, I like this better.

     

    Michael

     

    Thursday, May 29, 2008 4:30 PM