WPF中没有网格样式的模板属性? [英] no template property on grid style in WPF?
问题描述
我想将一个网格的所有内容移动到一个样式/模板/容器(不知道选择哪一个...),但我试图将其移动到样式中。
但问题是我得到的错误:无法找到'System.Windows.Controls.Grid'类型的样式属性'模板'。
我知道没有网格的模板属性,但我还将如何将网格内容移动到ResourceDirectory文件?
这是网格代码:
< Grid Grid.Column =0Grid.Row =0Margin =10,15,5 ,5>
< Border BorderThickness =7CornerRadius =4>
< Border.BorderBrush>
< SolidColorBrush Color =#73B2F5Opacity =0.5/>
< /Border.BorderBrush>
<网格>
< Grid.Background>
< SolidColorBrush Color =#73B2F5Opacity =0.5/>
< /Grid.Background>
< Grid.RowDefinitions>
< RowDefinition Height =30/>
< RowDefinition Height =1 */>
< /Grid.RowDefinitions>
< Button Name =CustomerButtonGrid.Row =1Grid.Column =0Width =40Height =40Content =CustomerClick =CustTabButton_Click>< /按钮>
< Button Name =BossButtonGrid.Row =1Width =40Height =40Content =BossMargin =23,206,23,114Click =BossTabButton_Click>< /按钮>
< / Grid>
< / Border>
< / Grid>
这是我在代码中移动代码后resourceDirectory中的代码:
< Style x:Key =LeftSidePanelTargetType ={x:Type Grid}>
< Setter Property =Template>
< Setter.Value>
< Border BorderThickness =7CornerRadius =4>
< Border.BorderBrush>
< SolidColorBrush Color =#73B2F5Opacity =0.5/>
< /Border.BorderBrush>
<网格>
< Grid.Background>
< SolidColorBrush Color =#73B2F5Opacity =0.5/>
< /Grid.Background>
< Grid.RowDefinitions>
< RowDefinition Height =30/>
< RowDefinition Height =1 */>
< /Grid.RowDefinitions>
< Button Name =CustomerButtonGrid.Row =1Grid.Column =0Width =40Height =40Content =CustomerClick =CustTabButton_Click>< /按钮>
< Button Name =BossButtonGrid.Row =1Width =40Height =40Content =BossMargin =23,206,23,114Click =BossTabButton_Click>< /按钮>
< / Grid>
< / Border>
< / Setter>
< / style>
我错过了什么?
ContentControl是您正在寻找的内容 -
< ContentControl Template ={StaticReosurce MyTemplate}>
在资源字典中声明您的模板 -
< ControlTemplate>
< Grid Grid.Column =0Grid.Row =0Margin =10,15,5,5>
< Border BorderThickness =7CornerRadius =4>
< Border.BorderBrush>
< SolidColorBrush Color =#73B2F5Opacity =0.5/>
< /Border.BorderBrush>
<网格>
< Grid.Background>
< SolidColorBrush Color =#73B2F5Opacity =0.5/>
< /Grid.Background>
< Grid.RowDefinitions>
< RowDefinition Height =30/>
< RowDefinition Height =1 */>
< /Grid.RowDefinitions>
< Button Name =CustomerButtonGrid.Row =1Grid.Column =0Width =40Height =40Content =CustomerClick =CustTabButton_Click>< /按钮>
< Button Name =BossButtonGrid.Row =1Width =40Height =40Content =BossMargin =23,206,23,114Click =BossTabButton_Click>< /按钮>
< / Grid>
< / Border>
< / Grid>
< / ControlTemplate>
如果您不知道ContentControl,请点击此链接 -
http://msdn.microsoft.com/zh-cn/library/system .windows.controls.contentcontrol.aspx
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'".
i know there is no template property for grid , but how else will i move the grid content to the ResourceDirectory file?
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?
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>
If you aren't aware of ContentControl, follow this link - http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx
这篇关于WPF中没有网格样式的模板属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!