Lasse O
Lasse O

Reputation: 349

Datatemplate binding

How can i achieve something like this:

<ListView Name="OverviewTitlesListView" ItemsSource="{Binding OverviewTitlesCollection}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Index" Width="60" DisplayMemberBinding="{Binding TitleIndex}"/>
                        <GridViewColumn Header="Start Time" Width="100" DisplayMemberBinding="{Binding StartTime}"/>
                        <GridViewColumn Header="End Time" Width="100" DisplayMemberBinding="{Binding EndTime}"/>
                        <GridViewColumn Header="Title Text" Width="550" DisplayMemberBinding="{Binding Text}"/>
                        <GridViewColumn Header="Approved" Width="80">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Name="Test"/>
                                    <DataTemplate.Triggers>
                                        <Trigger Property="{Binding IsApproved}" Value="true">
                                            <Setter TargetName="Test" Property="Text" Value="Approved"/>
                                        </Trigger>
                                        <Trigger Property="{Binding IsApproved}" Value="false">
                                            <Setter TargetName="Test" Property="Text" Value="Not Approved"/>
                                        </Trigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>

When IsApproved property changes on my object in OverviewTitlesCollection i want to control the text of the TextBlock. How can i control this by triggers in my datatemplate?

Upvotes: 1

Views: 601

Answers (2)

John Bowen
John Bowen

Reputation: 24453

Change your DataTemplate to this:

<DataTemplate>
    <TextBlock x:Name="Test" Text="Approved"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsApproved}" Value="False">
            <Setter TargetName="Test" Property="Text" Value="Not Approved"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

It is now using a DataTrigger to check against a Binding, rather than a Trigger which only supports individual properties on objects being templated like Controls. I also simplified it to use a default value and single trigger to switch back and forth. When the trigger becomes inactive it will switch back to the original state (Text="Approved").

Upvotes: 2

Jobi Joy
Jobi Joy

Reputation: 50038

As long as your IsApproved property raised INotifyPropertyChanged then your DataTrigger will work.

And use <DataTrigger Binding="{Binding IsApproved}". .. instead of Trigger

Upvotes: 0

Related Questions