Home > Enterprise >  How to Enable/Disable one item in ListView?
How to Enable/Disable one item in ListView?

Time:07-13

I have a below ListView in my WPF app

View:

<ListView SelectionMode="Single" Width="auto" Margin="0 20" SelectedItem="{Binding SelectedMiningModule}" ItemsSource="{Binding MiningModules}" MouseDoubleClick="AssignMiningModule_MouseDoubleClick">
                        <ListView.View>
                            <GridView >
                                <GridViewColumn Header="Module">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Grid IsEnabled="{Binding IsSelected}">
                                                <TextBlock Text="{Binding DisplayName}" />
                                            </Grid>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>

I am trying to enable/disable one list item based on binding. I tried adding IsEnabled="{Binding IsSelected}" to TextBlock as well but neither works...

Binding isn't a problem as if I just do IsEnabled="False" then still all items are enabled.

CodePudding user response:

You must configure the container and not the content:

<ListView>
  <ListView.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="IsEnabled"
              Value="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}">
    </Style>
  </ListView.ItemContainerStyle>
</ListView>
  • Related