简体   繁体   中英

WPF datagrid button in column header with header text - c#

I have a WPF datagrid and i want to apply a button on specific column headers. I managed to insert the button but the HeaderText disapears. What do i have to bind to my TextBlock that the headertext is "Match Ausdruck" in this example?

And how do i access this button?

My App.xaml:

<Style x:Key="columnHeaderButton" TargetType="DataGridColumnHeader">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel Orientation="Horizontal" Width="auto">
                    <TextBlock Text="Test" VerticalAlignment="Center" />
                    <Button Style="{StaticResource MyButton}" 
                        Width="16" 
                        Height="16" 
                        VerticalAlignment="Center">
                        <Button.Background>
                            <ImageBrush ImageSource="Resources/filter.png"/>
                        </Button.Background>
                    </Button>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

My MainWindow.xaml:

<DataGrid x:Name="dgVarConfig" 
    HorizontalAlignment="Left" 
    Margin="10,59,0,0" 
    VerticalAlignment="Top" 
    Height="403" 
    Width="1278" 
    AutoGenerateColumns="False" 
    CanUserAddRows="False" 
    CanUserDeleteRows="False" 
    CanUserResizeRows="False" 
    HeadersVisibility="Column">

    <DataGrid.Columns>
        <DataGridTextColumn 
            HeaderStyle="{StaticResource columnHeaderButton}"
            Width="auto" 
            Header="Match Ausdruck" 
            Binding="{Binding match_expression}">
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

As far as I understand your question, you have to adjust your ControlTemplate to have some ContentPresenter in it :

<ControlTemplate>
    <StackPanel Orientation="Horizontal" Width="auto">
        <ContentPresenter Content={TemplateBinding Content}" />
        <Button Style="{StaticResource MyButton}" Width="16" Height="16" VerticalAlignment="Center">
            <Button.Background>
                <ImageBrush ImageSource="Resources/filter.png"/>
            </Button.Background>
        </Button>
    </StackPanel>
</ControlTemplate>

Adjust the ContentPresenter properties with any TemplateBinding such as VerticalAlignment or HorizontalContentAlignment .

I know this is an old post but info on this was hard for me to find so here's what worked for me. First I'm assuming the TextBlock you are asking about is the one that currently displays "Test" and that other than not displaying the column name everything else is working ok. In that case you can just set the Text property like this:

<TextBlock Text="{Binding}"/>

On a side note, and I know this isn't what you're asking, you could also have made the button display the column text like this:

<Button>
    <TextBlock Text="{Binding}"/>
</Button>

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