views:

320

answers:

1

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>

+1  A: 

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

Stephen Wrighton
Awesome, thanks Stephen. I need to learn more about converters, I see them referred to a lot. Are there any good examples or posts you'd recommend?
MarkB
There's a sample of the BooleanToVisibility converter here: http://www.wpftutorial.net/DataBindingOverview.html This is definitely the way to go.
Anderson Imes
Thanks Anderson and Stephen for the links, good stuff!
MarkB