Harald
Harald

Reputation: 1037

ListView changing CheckBox visibility via DataTrigger

I have a ListView where each line shows a CheckBox and 2 TextBlocks. It's bound to an ObservableCollection where each item has a IsDuplicate property. All the data binding works and I have a DataTrigger that turns the entire line red when the property is true. That works.

My problem is this: In addition to turning the line red I also want change the Visibility property of the CheckBox (but only the CheckBox) when IsDuplicate is true.

The following code turns the line red but the CheckBox Visibility remains unchanged. I tried a TargetName on the setter but I get an error telling me "TargetName property cannot be set on a Style Setter."

How can I achieve that only the CheckBox Visibility property gets set to Hidden when IsDuplicate is true?

<ListView ItemsSource="{Binding Tasks}" IsSynchronizedWithCurrentItem="True" SizeChanged="ListViewSizeChanged" Loaded="ListViewLoaded" >
<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsDuplicate}" Value="True">
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="CheckBox.Visibility" Value="Collapsed" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>
<ListView.View>
    <GridView>
        <GridView.Columns>
            <GridViewColumn Width="30" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate x:Name="Checker" >
                        <CheckBox IsChecked="{Binding IsSelected}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="160" Header="File" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding FileName}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="260" Header="URL" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding VideoUrl}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="300" Header="Download URL" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding DownloadUrl}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView.Columns>
    </GridView>
</ListView.View>

Upvotes: 1

Views: 1826

Answers (1)

hholtij
hholtij

Reputation: 2936

Replace your DataTemplate for the CheckBox with the following. That should do the trick:

<DataTemplate>
    <CheckBox IsChecked="{Binding IsSelected}" >
        <CheckBox.Style>
            <Style TargetType="CheckBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsDuplicate}" Value="True">
                        <Setter Property="Visibility" Value="Hidden" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </CheckBox.Style>
    </CheckBox>
</DataTemplate>

Upvotes: 2

Related Questions