wpf datagrid checkbox column header de/selects all
I'm trying to create a datagrid with a checkbox column that has a header with a checkbox that selects and deselects the rows' checkboxes. Also I'm attempting to do it with no behind code. So far I've been partialy successful in that I used the datagrid's tag as a middle man in the binding, and could de/select the rows' checkboxes from the header checkbox using data triggers but once I check a row the binding in that row 'dies' and it is no longer effected by the header. Also, I already tried it with one way bindings instead of data triggers and got the same result... Does anyone know of a way to keep the binding alive or pheraps another to achive the effect I'm looking for inxaml only.
header:
<CheckBox IsChecked="{binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}, Path=Tag}"/>
column template:
<CheckBox>
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Style.Triggers>
<DataTrigger Binding="{binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}, Path=Tag}" Value="True">
<Setter Property="IsChecked" Value="True"/>
开发者_JAVA百科 </DataTrigger>
<DataTrigger Binding="{binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}, Path=Tag}" Value="False">
<Setter Property="IsChecked" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
That is actually very good idea to solve that problem. I can't see why that shouldn't work.
That said, there are a few things to try:
Place your header checkbox and the row checkbox on a window normally (not inside a DataGrid) to see what effect that has.
The other option is to try naming the Header checkbox (for example <CheckBox Name="cbxHeader"
) and then binding to it with {Binding ElementName=cbxHeader Path=Tag}
.
Also, check the "Output" window in Visual Studio while you are testing (while it is running, Debug > Windows > Output; it might appear empty but just scroll up) for any messages about why the bindings are failing.
You can also use a bogus ValueConverter
to try and see if the bindings are still firing.
精彩评论