简体   繁体   中英

C# -WPF DataGridColumnHeader in 2 Rows

I'm trying to create a DataGridColumnHeader with 2 Rows. I want that it look this way:

在此处输入图片说明

HeaderText and ID are are AttachedProperties at a custom DataGridColumn. I wanted to show both properties with the following template for DataGridColumnHeader:

<Style TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Margin="0">
                    <Border Margin="0"
                            BorderBrush="Black"
                            BorderThickness="0,0,0,1">
                        <TextBlock Text="{Binding Path=HeaderText, RelativeSource={RelativeSource AncestorType={x:Type ctrls:MyDataGridColumn}}}" />
                    </Border>
                    <TextBlock Text="{Binding Path=ID, RelativeSource={RelativeSource AncestorType={x:Type ctrls:MyDataGridColumn}}}" />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Example for my custom column:
<ctrls:MyDataGridColumn HeaderText="some text" ID="1" />

HeaderText and ID wont be displayed... any tips?

When working with DataGridColumns I typically use Window Resources/DataTemplates rather than styles. For example:

<Window.Resources>
     <DataTemplate x:Key="TemplateName">
         <insert stackpanel & TextBlocks here/>
     </DataTemplate>
</Window.Resources>

And then, in your GridView, set your column like this:

<GridViewColumn CellTemplate="{StaticResource TemplateName}"/>

I apologize if this isn't the answer you're looking for. But this is how I have always accomplished the goal you stated. Let me know if you have any questions!

EDIT: I've also never used bindings like that. Let me provide an example:

Here is a datatemplate for a gridviewcolumn that has a checkbox that is bound to a bool property in the Class that is in the items source:

<DataTemplate x:Key="IssueTemplate">
    <CheckBox HorizontalAlignment="Center" IsChecked="{Binding IssueMe, Mode=TwoWay}"/>
</DataTemplate>

EDIT 2: I now see you're talking about headers which makes my answer pointless. My apologies. I usually set headers on a case by case basis:

<GridViewColumn CellTemplate="{StaticResource IssueTemplate}">
    <GridViewColumnHeader HorizontalAlignment="Center">
        <StackPanel HorizontalAlignment="Center">
            <TextBlock Text="Issue" HorizontalAlignment="Center"/>
                <Viewbox>
                    <Button Content="Check/Uncheck All" FontSize="6" Margin="5 0 5 0" Command="{Binding CheckClick}"/>
                </Viewbox>
        </StackPanel>
    </GridViewColumnHeader>
</GridViewColumn>

If you need it in a style then I'm afraid I can't provide any more help. My best suggestion would be to take a look at your bindings; something looks off with them IMO.

Good luck!

Your present Binding wont work as DataGridColumn is not a Visual , so it won't participate in VisualTree , so you cannot traverse it. It is merely an information holder, nothing else. DataGridColumnHeader instead contains a property Column which gives you access to the DataGridColumn .

So, do this for both properties :

<TextBlock Text="{Binding Path=Column.HeaderText, RelativeSource={RelativeSource AncestorType={x:Type DataGridColumnHeader}}}" />

Please tell if this solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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