gpt4 book ai didi

wpf - WPF 中的网格样式没有模板属性?

转载 作者:行者123 更新时间:2023-12-03 11:38:02 25 4
gpt4 key购买 nike

我想移动一个 Grid 的所有内容到样式/模板/容器(不知道选择哪个...),但我尝试将其移动到 Style .
但问题是我收到错误:

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


我知道没有 Template Grid 的属性(property),但我将如何将网格内容移动到 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>
这是我将代码移到那里后资源目录中的代码:
<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 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

关于wpf - WPF 中的网格样式没有模板属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9015778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com