开发者

Make individual checkboxes on WPF datagrid not visible

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>


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

  • http://blogs.imeta.co.uk/FMoreno/archive/2008/11/07/498.aspx
  • http://www.switchonthecode.com/tutorials/wpf-tutorial-binding-converters
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜