Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Enable/Disable one item in ListView?

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading