简体   繁体   English

DataGrid 标头中的 WPF 数据绑定

[英]WPF DataBinding in a DataGrid Header

I'm trying to bind a ViewModel property to a Checkbox in the header of a DataGrid.我正在尝试将 ViewModel 属性绑定到 DataGrid 标头中的 Checkbox。

The checkbox binds just fine if I stick it randomly in the window, but if its in the header of the datagrid, it doesn't bind in either direction.如果我将它随机粘贴在窗口中,复选框绑定得很好,但如果它在数据网格的标题中,它不会在任何一个方向绑定。

The data in the DataGrid binds fine as well. DataGrid 中的数据也可以很好地绑定。

The issue seems to be that the HeaderTemplate isn't binding to the main view model.问题似乎是 HeaderTemplate 没有绑定到主视图模型。 I assume its binding ItemSource.我假设它绑定了 ItemSource。

How do I bind to the view model in the header?如何绑定到标题中的视图模型?

   <DataGrid ItemsSource="{Binding Channels}" AlternationCount="2" Grid.IsSharedSizeScope="True" AutoGenerateColumns="False" AlternatingRowBackground="{StaticResource GroupBackgroundBrush}" SelectedIndex="{Binding Path=CursorChannelInt}" >
        <DataGrid.Columns>
              <DataGridTemplateColumn>
                    <DataGridTemplateColumn.HeaderTemplate>
                           <DataTemplate>                                    
                                <CheckBox IsChecked="{Binding Path=Test}">Test Chkbox</CheckBox>
                           </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                           <DataTemplate>
                               <TextBlock Text="{Binding Path=stuff}"/>
                          </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
              </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

This works, by going to the Window, getting its DataContext, and going from there.这是可行的,方法是转到 Window,获取其 DataContext,然后从那里开始。 Is there a better way?有没有更好的办法?

<CheckBox IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Test}">Test Chkbox</CheckBox>

The above answers were not working in my case, so I solved using Initialized event trigger上述答案在我的情况下不起作用,所以我使用Initialized event trigger 解决了

.xaml .xaml

<DataGridTemplateColumn.HeaderTemplate>
   <DataTemplate>                                    
   <CheckBox Initialized="CheckBox_Initialized" IsChecked="False">Test Chkbox</CheckBox>
   </DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>

.xaml.cs .xaml.cs

private CheckBox cb_All = null;
private void CheckBox_Initialized(object sender, EventArgs e)
{
   cb_All = (CheckBox)sender;
}

private void Function()
{
   if(cb_All != null)
      cb_All.IsChecked = true; //or false 
}

It is sooo late, spending several years.. but I hope this may help to anyone:)太晚了,花了几年时间..但我希望这可以帮助任何人:)

if the property is not in the collection, you have perhaps a nice answer, other rewrite for the same would be to use the ElementName to shorten the binding syntax如果该属性不在集合中,您可能有一个很好的答案,其他相同的重写是使用ElementName来缩短绑定语法

sample样本

<DataGrid ItemsSource="{Binding Channels}" AlternationCount="2" Grid.IsSharedSizeScope="True" AutoGenerateColumns="False" AlternatingRowBackground="{StaticResource GroupBackgroundBrush}" SelectedIndex="{Binding Path=CursorChannelInt}" 
          x:Name="dGrid">
    <DataGrid.Columns>
          <DataGridTemplateColumn>
                <DataGridTemplateColumn.HeaderTemplate>
                       <DataTemplate>                                    
                            <CheckBox IsChecked="{Binding DataContext.Test, ElementName=dGrid}">Test Chkbox</CheckBox>
                       </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                       <DataTemplate>
                           <TextBlock Text="{Binding Path=stuff}"/>
                      </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

above sample is based on the assumption that the property Test is in the same VM as Channels property.上面的示例基于属性TestChannels属性在同一 VM 中的假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM