WPF 中的网格样式没有模板属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9015778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
no template property on grid style in WPF?
提问by Rodniko
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 : "Cannot find the Style Property 'Template' on the type 'System.Windows.Controls.Grid'".
但问题是我收到错误消息:“在‘System.Windows.Controls.Grid’类型上找不到样式属性‘模板’”。
i know there is no template property for grid , but how else will i move the grid content to the ResourceDirectory file?
我知道 grid 没有模板属性,但是我将如何将网格内容移动到 ResourceDirectory 文件?
This is the Grid code:
这是网格代码:
<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>
What did i miss?
我错过了什么?
回答by Rohit Vats
ContentControl is what you are looking for -
ContentControl 是您正在寻找的 -
<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>
If you aren't aware of ContentControl, follow this link - http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx
如果您不知道 ContentControl,请点击此链接 - http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx