开发者

wrapping a data grid with a toolbox

开发者 https://www.devze.com 2023-04-06 06:36 出处:网络
I got a wpf application. I want al开发者_高级运维l my data grids in application to have a set of buttons above them.

I got a wpf application.

I want al开发者_高级运维l my data grids in application to have a set of buttons above them.

Tried to use decorator and adorner without success(the dataGrid stopped showing rows)

Any suggestions?


Given that you're wanting to have functionality behind the toolbox buttons (which I assume will require a reference to the grid) it probably makes sense to inherit from a HeaderedContentControl for this. This does mean that you can put any content in the control, but it would be possible to put override the metadata to add validation for this.

Anywhere, here's the xaml:

<!-- ToolBoxGridControl.xaml -->
<HeaderedContentControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
x:Class="WpfApplication3.ToolBoxGridControl">   
<HeaderedContentControl.Header>
    <StackPanel Orientation="Horizontal">
        <Button/>
        <Button/>
        <Button/>
    </StackPanel>
</HeaderedContentControl.Header>
<HeaderedContentControl.Template>
    <ControlTemplate TargetType="HeaderedContentControl">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <ContentControl Grid.Row="0" Content="{TemplateBinding Header}"/>
            <ContentControl Grid.Row="1" Content="{TemplateBinding Content}"/>
        </Grid>
    </ControlTemplate>
</HeaderedContentControl.Template>
</HeaderedContentControl>

And the simple code-behind (where you can put your toolbox implementation).

public partial class ToolBoxGridControl : HeaderedContentControl
{       
    private DataGrid DataGrid { get { return (DataGrid)Content; } }

    public ToolBoxGridControl()
    {
        this.InitializeComponent();
    }
}

To actually use, you can just add the following to your XAML with your data grid

<local:ToolBoxGridControl>
    <DataGrid/> 
</local:ToolBoxGridControl>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号