WPF中的网格样式没有模板属性? [英] no template property on grid style in WPF?

查看:25
本文介绍了WPF中的网格样式没有模板属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Grid 的所有内容移动到样式/模板/容器(不知道该选择哪个...),但我试图将其移动到 <代码>样式.

i want to move all the content of a Grid to a style/template/Container (don't know which one to choose...), but I tried to move it to a Style.

但问题是我得到了错误:

but the problem is I get the error :

"在类型上找不到样式属性模板"'System.Windows.Controls.Grid'"

"Cannot find the Style Property 'Template' on the type 'System.Windows.Controls.Grid'"

我知道 Grid 没有 Template 属性,但我还能如何将网格内容移动到 ResourceDirectory 文件?

i know there is no Template property for Grid , but how else will I move the grid content to the ResourceDirectory file?

这是网格代码:

<Grid  Grid.Column="0"  Grid.Row="0" Margin="10,15,5,5" >
        
        <Border BorderThickness="7" CornerRadius="4">
            <Border.BorderBrush>
                <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
            </Border.BorderBrush>
            <Grid>
                <Grid.Background>
                    <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
                </Grid.Background>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Button Name="CustomerButton" Grid.Row="1" Grid.Column="0" Width="40" Height="40" Content="Customer" Click="CustTabButton_Click" ></Button>
                <Button Name="BossButton" Grid.Row="1" Width="40" Height="40" Content="Boss" Margin="23,206,23,114" Click="BossTabButton_Click"></Button>
            </Grid>
        </Border>
        
    </Grid>

这是我将代码移到资源目录后的代码:

This is the code in the resourceDirectory after i move the code there:

<Style x:Key="LeftSidePanel" TargetType="{x:Type Grid}">
    <Setter Property="Margin" Value="10,15,5,5" />
    <Setter Property="Template">
        <Setter.Value>
            <Border BorderThickness="7" CornerRadius="4">
                <Border.BorderBrush>
                    <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
                </Border.BorderBrush>
                <Grid>
                    <Grid.Background>
                        <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
                    </Grid.Background>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30"/>
                        <RowDefinition Height="1*"/>
                    </Grid.RowDefinitions>
                    <Button Name="CustomerButton" Grid.Row="1" Grid.Column="0" Width="40" Height="40" Content="Customer" Click="CustTabButton_Click"></Button>
                    <Button Name="BossButton" Grid.Row="1" Width="40" Height="40" Content="Boss" Margin="23,206,23,114" Click="BossTabButton_Click"></Button>
                </Grid>
            </Border>
        </Setter.Value>
    </Setter>
</Style>

我错过了什么?

推荐答案

ContentControl 就是你要找的 -

ContentControl is what you are looking for -

<ContentControl Template="{StaticReosurce MyTemplate}">

像这样在资源字典中声明你的模板 -

Declare your template in the resource dictionary like this -

<ControlTemplate>
   <Grid  Grid.Column="0"  Grid.Row="0" Margin="10,15,5,5" >
        <Border BorderThickness="7" CornerRadius="4">
            <Border.BorderBrush>
                <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
            </Border.BorderBrush>
            <Grid>
                <Grid.Background>
                    <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
                </Grid.Background>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Button Name="CustomerButton" Grid.Row="1" Grid.Column="0" Width="40" Height="40" Content="Customer" Click="CustTabButton_Click" ></Button>
                <Button Name="BossButton" Grid.Row="1" Width="40" Height="40" Content="Boss" Margin="23,206,23,114" Click="BossTabButton_Click"></Button>
            </Grid>
        </Border>
    </Grid>

</ControlTemplate>

如果您不了解 ContentControl,请点击此链接 -https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.contentcontrol

If you aren't aware of ContentControl, follow this link - https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.contentcontrol

这篇关于WPF中的网格样式没有模板属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆