首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >WPF中的网格样式没有模板属性吗?

WPF中的网格样式没有模板属性吗?
EN

Stack Overflow用户
提问于 2012-01-26 17:00:45
回答 1查看 31.5K关注 0票数 25

我想将Grid的所有内容移动到style/template/Container (不知道选择哪一个...),但我尝试将其移动到Style

但问题是我得到了错误:

“在类型‘System.Windows.Controls.Grid’上找不到样式属性'Template‘”

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

这是网格代码:

<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>

这是我将代码移到resourceDirectory中之后的代码:

<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>

我错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-26 17:56:41

ContentControl就是你要找的东西-

<ContentControl Template="{StaticReosurce MyTemplate}">

在资源字典中声明您的模板,如下所示-

<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

票数 38
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9015778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档