no template property on grid style in WPF?

*爱你&永不变心* 提交于 2020-02-18 05:47:10

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/9015778/no-template-property-on-grid-style-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!