shtkuh
shtkuh

Reputation: 459

DataTrigger is not firing

I have array of class Person in ViewModel and I want to show their names in table. I have also column with checkboxes. This is my View part:

<Grid>
    <Grid.Resources>
        <Style x:Key="CheckBoxStyle" TargetType="{x:Type Control}">
            <Setter Property="Visibility" Value="Visible"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelectionAllowed}" Value="False">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <ListView ItemsSource="{Binding Persons}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="40">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox  Style="{StaticResource CheckBoxStyle}"
                                                .........................
                                                .... some logic here .... 
                                                ......................./>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="140" 
                            Header="Number" 
                            DisplayMemberBinding="{Binding Path=Name}" />
        </GridView>
    </ListView.View>
    </ListView>
</Grid>

I want to show/hide checkboxes according to value of IsSelectionAllowed boolean variable. Why DataTrigger is not firing?

Upvotes: 0

Views: 332

Answers (1)

Phil
Phil

Reputation: 43011

Assuming IsSelectionAllowed is a property on the view model set on the data context, you will need a relative source binding - hopefully this is correct without any testing:

<DataTrigger Binding="{Binding Path=IsSelectionAllowed, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" Value="False">
   <Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>

The binding of IsSelectionAllowed in your code is to the Person type.

Upvotes: 1

Related Questions