将按钮添加到有界DataGridColumn

时间:2017-04-28 09:52:20

标签: wpf xaml

我得到了这个XAML代码:

<Grid>
    <Grid.Resources>
        <XmlDataProvider x:Key="SlData" Source="test.xml" XPath="/sls/sl"/>
    </Grid.Resources>
    <DataGrid x:Name="dgridEmp" Style="{DynamicResource Datag1}" DataContext="{StaticResource SlData}" ItemsSource="{Binding XPath=/sls/sl}" AutoGenerateColumns="False" Margin="1,145,7,-506" FontSize="15" IsReadOnly="True" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="bencd" Binding="{Binding XPath=benCd}" Width="72" >
            </DataGridTextColumn>
            <DataGridTextColumn Header="slpos" Binding="{Binding XPath=slpos}" Width="40" />
            <DataGridTextColumn Header="slNr" Binding="{Binding XPath=slNr}" Width="72" />
            <DataGridTextColumn Header="stufe" Binding="{Binding XPath=stufe}" Width="40" />
            <DataGridTextColumn Header="spJn" Binding="{Binding XPath=spJN}" Width="40" />
            <DataGridTextColumn Header="slNrHpt" Binding="{Binding XPath=slNrHpt}" Width="72" />
            <DataGridTextColumn Header="me" Binding="{Binding XPath=menge}" Width="30" />
            <DataGridTextColumn Header="bez" Binding="{Binding XPath=bez}" Width="250" />
            <DataGridTextColumn Header="bezkurz" Binding="{Binding XPath=bezkurz}" Width="100" />
            <DataGridTextColumn Header="bezE" Binding="{Binding XPath=bezE}" Width="100" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

我想要的是向标题&#34; bencd&#34;添加一个Button Right到Datagridtextcolumn,使它看起来像这样(使用这个蓝色过滤器按钮在正确的位置):

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将Header属性设置为您想要的任何元素,例如StackPanel TextBlockButton

<DataGridTextColumn Binding="{Binding XPath=benCd}" Width="72" >
    <DataGridTextColumn.Header>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="bencd"/>
            <Button Content="Filter" />
        </StackPanel>
    </DataGridTextColumn.Header>
</DataGridTextColumn>

当然,您需要设置Button的样式,使其看起来像&#34;过滤器&#34;按钮,但这是另一个故事。