R – Make individual checkboxes on WPF datagrid not visible

mvvmwpfwpfdatagrid

I have a datagrid bound to a CollectionViewSource view in my view model. The datagrid has a CheckBoxColumn and three TextColumns, each bound to properties in the items in the view. I also have a bool property "Enabled" in the view item that I would like to use to make checkboxes in individual rows not visible. As an added bonus, it would be great to also change the foreground color of the other three columns for those rows too. But the main thing is to not let the user check the checkboxes in the rows where Enabled == false.

EDIT: Based on Stephen's and Andrew's links below, I have attempted to add a value converter as in the code below. For some reason though, it is not working. I am testing now on a CollectionView view that has 90 items in which the Enabled property of all but one of the items is false. So I am expecting 89 rows with invisible checkboxes and 1 row with a visible checkbox. However, all 90 rows have visible checkboxes. Any ideas?

<tk:DataGrid x:Name ="gridClaims" 
           Grid.Row="0"
           AutoGenerateColumns="False" 
           ItemsSource="{Binding ClaimViewModels.View}"
           Width="350" 
           HrizontalAlignment="Left">

 <tk:DataGrid.Resources>
      <BooleanToVisibilityConverter x:Key="boolToVis" />
 </tk:DataGrid.Resources>

 <tk:DataGrid.Columns>
      <tk:DataGridCheckBoxColumn 
           CellStyle="{StaticResource SingleClickEditing}"

           Binding="{Binding Path=Selected}" 
           Visibility="{Binding Path=Enabled, Converter={StaticResource boolToVis}}"

           CanUserSort="False" />

 </tk:DataGrid.Columns>

Best Answer

For the Visibility function aspect, you would need a BooleanToVisbility converter which you'll use and bind the Visible state of hte datagrid to the Enabled property in your datacontext object.

Additionally, the Foreground color could be handled in the exact same way.

EDIT: Add Links to Converters

Related Topic