1

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>
Steve
  • 976
  • 5
  • 15
Tommehh
  • 872
  • 1
  • 17
  • 44

2 Answers2

1

As far as I understand your question, you have to adjust your ControlTemplate to have some ContentPresenterin 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.

xum59
  • 841
  • 9
  • 16
  • sorry, i dont really understand this – Tommehh Sep 23 '15 at 07:47
  • If you replace the `TextBlock` with its `Text` property set to "test" with a `ContentPresenter` like shown here, it will use the value (string will default to a `TextBlock`) provided in the `Header` property of your column. – xum59 Sep 23 '15 at 07:52
0

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>
LorneCash
  • 1,446
  • 2
  • 16
  • 30