none
WPF GridView Checkbox - How to enable/disable or hide/show checkbox in certain data row?

    Question

  • I am trying to build an application that make use of WPF's GridView and checkbox. 

    I found samples to have checkboxes in GridView's column and got them working with all triggers and everything.  Thing I need to do is be able to disable/enable or even hide/show a certain checkboxes (depending on some conditions).  Another word, for some rows, I do not want the user click on the checkbox.

    Below is the XAML I used to create checkbox column (let me know if you need more info):

    ...

    <Window.Resources>
       <DataTemplate x:Key="AutoReleaseTemplate">
          <CheckBox IsChecked="{Binding Path=IsAutoSelected}" x:Name="checkBoxAuto" IsEnabled="False" />
       </DataTemplate>
    </Window.Resources>

    ...

    </ListView>
    <
    ListView.View>
       <GridView ColumnHeaderTemplate="{StaticResource BlueHeader}">
           <GridViewColumn Header="ManualRelease" CellTemplate="{StaticResource ManualReleaseTemplate}" Width="100"/>
       </GridView>
    </ListView.View>
    </ListView>


    ...

    Thank you for any help.

    Friday, March 06, 2009 9:01 PM

Answers

  • Got it works the way I want it.  Thank you a million times.

    Final code here:

    ...
    <Window.Resources>
       <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
       <
    DataTemplate x:Key="AutoReleaseTemplate">
          <CheckBox IsEnabled="{Binding Path=IsManualEnabled}" IsChecked="{Binding Path=IsManualSelected}" 
                   Visibility="{Binding Path=IsManualVisibled, Converter={StaticResource boolToVisibilityConverter}}"
                   x:Name="checkBoxManual" />
       </DataTemplate>
    </Window.Resources>

    ...

    <ListView>
    <ListView.View
    >
       <GridView ColumnHeaderTemplate="{StaticResource BlueHeader
    }">
           <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Path=Age}" Width="100"/>
           <GridViewColumn Header="ManualRelease" CellTemplate="{StaticResource ManualReleaseTemplate}" Width="100"/>
       </
    GridView>
    </ListView.View
    >
    </ListView>


    ...

    • Marked as answer by tungxle Saturday, March 07, 2009 2:07 PM
    Saturday, March 07, 2009 2:07 PM

All replies

  • Well first thing that comes into my mind is that, is the "disable/enable or even hide/show a certain checkboxes (depending on some conditions)" associated with the DataContext for the ListViewItem, cause if it is just hook up Trigger to Enable/Disable or Hide or show visibility.

    If your object bound to the ListViewItem (The one that you are getting the 'IsAutoSelelcted' from) does not know about this information, then how are you getting in?

    Need a bit more information for the criteria to see how they are related.
    One more thing, when you are binding, try not to explicitly set properties like IsEnabled="False" because it will cause an overwrite and you will not see the correct binding value.

    I hope the first part gets you going, if not post more info please.

    -noorbakhsh
    Friday, March 06, 2009 9:45 PM
  • Thank you for the answer.

    Here is some more info:

    </ListView>
    <ListView.View>
       <GridView ColumnHeaderTemplate="{StaticResource BlueHeader
    }">
           <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Path=Age}" Width="100"/>
           <GridViewColumn Header="ManualRelease" CellTemplate="{StaticResource ManualReleaseTemplate}" Width="100"/>
       </
    GridView
    >
    </ListView.View
    >
    </ListView>



    Age column was coded to added with many other columns in program codes, but I will put that column in here for understanding. 

    Logic is:
    - if Age < 16, checkbox is hidden,
    - if Age >= 16 AND Age < 21, checkbox will be shown but disable (with checked status)
    - if Age >= 21, checkbox will be shown and enable (default is unchecked) - user can toggle this check box to change IsAutoSelected

    Hope this will be cleared.  If
    Saturday, March 07, 2009 6:01 AM
  • I am able to Enable/Disable the checkbox using

    <DataTemplate x:Key="AutoReleaseTemplate">
        <CheckBox IsEnabled="{Binding Path=IsAutoEnabled}" IsChecked="{Binding Path=IsAutoSelected}" x:Name="checkBoxAuto" />
    </DataTemplate>

    But there is nothing on visibility.  Is is possible?  One down, one to go.

    Saturday, March 07, 2009 7:20 AM
  • you can write or search for BoolToVisibilityConverter and use that assuming a boolean property is going to tell if the checkbox is visible or not
    http://leeontech.wordpress.com/
    Saturday, March 07, 2009 10:56 AM
    Moderator
  • Got it works the way I want it.  Thank you a million times.

    Final code here:

    ...
    <Window.Resources>
       <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
       <
    DataTemplate x:Key="AutoReleaseTemplate">
          <CheckBox IsEnabled="{Binding Path=IsManualEnabled}" IsChecked="{Binding Path=IsManualSelected}" 
                   Visibility="{Binding Path=IsManualVisibled, Converter={StaticResource boolToVisibilityConverter}}"
                   x:Name="checkBoxManual" />
       </DataTemplate>
    </Window.Resources>

    ...

    <ListView>
    <ListView.View
    >
       <GridView ColumnHeaderTemplate="{StaticResource BlueHeader
    }">
           <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Path=Age}" Width="100"/>
           <GridViewColumn Header="ManualRelease" CellTemplate="{StaticResource ManualReleaseTemplate}" Width="100"/>
       </
    GridView>
    </ListView.View
    >
    </ListView>


    ...

    • Marked as answer by tungxle Saturday, March 07, 2009 2:07 PM
    Saturday, March 07, 2009 2:07 PM
  • Can you please share code on C# side that how you are showing and hiding the specific checkBox.

    Vipul Mistry Embedded Engineer www.eInfochips.com

    Tuesday, May 15, 2012 3:11 PM