How to set the DataContext on a DataGrid Column He

2019-01-23 08:30发布

In my Silverlight 3 user control I am showing a basic DataGrid control. I need to generate the columns programmatically as follows:

Style headerStyle = (Style)Resources["ColumnHeaderStyle"];
DataGridTextColumn col = new DataGridTextColumn();
col.HeaderStyle = headerStyle;
dataGrid.Columns.Add(col);

The style is defined as follows:

<Style x:Name="ColumnStyle" x:Key="ColumnHeaderStyle" 
       TargetType="prim:DataGridColumnHeader">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Loaded="StackPanel_Loaded">
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text="{Binding Data}" />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

I want to set the data context of the header to a "header" object (with "Name" and "Data" properties which are referenced in the DataTemplate). Unfortunately, I cannot use the StackPanel_Loaded event as suggested elsewhere, because the event handler is also called when the user starts a column drag&drop operation.

What is the correct way of setting the DataContext of a DataGrid column header?

3条回答
狗以群分
2楼-- · 2019-01-23 08:38

Here's how you would do it in XAML (this works in WPF; not sure if it works in SL)

<DataGridTextColumn Binding="{Binding Path=Discount}">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.DiscountHeader}" />
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
查看更多
Deceive 欺骗
3楼-- · 2019-01-23 08:44

Turns out that one can use the Header property (which is of type Object) as the DataContext for the DataTemplate (set as shown above):

Style headerStyle = (Style)Resources["ColumnHeaderStyle"];
DataGridTextColumn col = new DataGridTextColumn();
col.HeaderStyle = headerStyle;
col.Header = myHeaderDataContext; // DataContext for ColumnHeaderStyle
dataGrid.Columns.Add(col);
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-23 08:44

Based on Matt's answer, I came up with the solution of binding the header on the DataGridCellsPanel which in Snoop appeared to have the correct data context :

  <DataGridTemplateColumn.HeaderStyle>
     <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource CenterAlignedColumnHeaderStyle}">
         <Setter Property="Content" Value="{Binding Path=DataContext.Location, RelativeSource={RelativeSource AncestorType={x:Type DataGridCellsPanel}}}" />
      </Style>
  </DataGridTemplateColumn.HeaderStyle>

And this is non intrusive in the way that you can still inherits from custom styled headers (see exemple above) or event the base column header style:

 <DataGridTemplateColumn.HeaderStyle>
     <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
     <Setter Property="Content" Value="{Binding Path=DataContext.Location, RelativeSource={RelativeSource AncestorType={x:Type DataGridCellsPanel}}}" />
     </Style>
 </DataGridTemplateColumn.HeaderStyle>

This solution has the advantage of being pure and clean XAML and to refer to the closest ancestor holding the correct datacontext rather than trying to reach datacontext of top hierarchy elements like UserControl.

查看更多
登录 后发表回答