Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no template property on grid style in WPF?

Tags:

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?

like image 940
Rodniko Avatar asked Jan 26 '12 09:01

Rodniko


1 Answers

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 - https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.contentcontrol

like image 61
Rohit Vats Avatar answered Nov 02 '22 16:11

Rohit Vats