我想将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>
我错过了什么?
发布于 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
https://stackoverflow.com/questions/9015778
复制相似问题