views:

2954

answers:

3
+4  Q: 

WPF Triggers

Hi,

I'm trying to set a trigger to display a block of text when the value i get for the cell is a certain type.

I have successfully managed to display an image in the same situation, but in this circumstance i don't want an image, but some text.

Have commented out lines in order to test.try to make it work. The commented out code works ! The textblock text=xxx inside it, doesn't.

This is my attempts(s)

<wpfToolkit:DataGridTemplateColumn Header="P" Width="20">
    <wpfToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <!-- <DataTemplate.Triggers> -->
            <!-- <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1"> -->
            <TextBlock Text="XXX" />
            <!-- </DataTrigger> -->
            <!-- <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2"> -->
            <!-- <Setter TargetName="cablePrecedenceIndicatorImage" Property="Source" Value="Resources\Images\small_exclamation_mark.png"/> -->
            <!-- </DataTrigger> -->
            <!-- </DataTemplate.Triggers> -->
        </DataTemplate>
    </wpfToolkit:DataGridTemplateColumn.CellTemplate>
</wpfToolkit:DataGridTemplateColumn>
A: 

You need to change the type of the DataTemplate, based on your trigger, from an Image to a TextBox.

<DataTemplate>
    <ContentPresenter x:Name="Presenter" />

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1">
            <Setter TargetName="Presenter" Property="Content">
                <Setter.Value>
                    <TextBlock Text="XXX" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2">
            <Setter TargetName="cablePrecedenceIndicatorImage" Property="Source" Value="Resources\Images\small_exclamation_mark.png"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>
Cameron MacFarland
The tag 'DataGridTemplateColumn.Resources' does not exist in XML namespace 'clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit'
mike
close !'System.Windows.Controls.TextBlock' is not a valid value for 'Setter.Value'; values derived from Visual or ContentElement are not supported.
mike
+2  A: 

Set the content of the DataTemplate to the TextBlock and set the visibility based on the trigger. I'm not sure what the second DataTrigger is for because it is referring to a target name that does not exist in the current scope, so I don't know how this fits in.

<DataTemplate>
    <TextBlock x:Name="block" Text="XXX" Visibility="Collapsed"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1">
            <Setter TargetName="block" Property="Visibility" Value="Visible"/>
        </DataTrigger>
        <!--<DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2">
            <Setter TargetName="cablePrecedenceIndicatorImage" Property="Source" Value="Resources\Images\small_exclamation_mark.png"/>
        </DataTrigger>-->
    </DataTemplate.Triggers>
</DataTemplate>
Josh G
Awesome, thanks ! See my 'answer' at bottom!
mike
+1  A: 

Brilliant ! Thanks heaps :)

This is what I ended up with. If PrecedenceIndicator =1, display !, if PrecendenceIndicator =2, display !!.

<wpfToolkit:DataGridTemplateColumn Header="A" Width="20">
    <wpfToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock x:Name="block1" Text="&#x21;" Visibility="Collapsed"/>
                <TextBlock x:Name="block2" Text="&#x21; &#x21;" Visibility="Collapsed"/>
            </TextBlock>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1">
                    <Setter TargetName="block1" Property="Visibility" Value="Visible"/>
                </DataTrigger>
               <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2">
                    <Setter TargetName="block2" Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </wpfToolkit:DataGridTemplateColumn.CellTemplate>
</wpfToolkit:DataGridTemplateColumn>
mike
Looks great! Glad it worked out.
Josh G