нет шаблона в стиле сетки в WPF? - PullRequest
20 голосов
/ 26 января 2012

Я хочу переместить все содержимое сетки в стиль / шаблон / контейнер (не знаю, какой из них выбрать ...), но я попытался переместить его в стиль.

но проблема в том, что я получаю сообщение об ошибке: «Не удается найти свойство стиля« Шаблон »для типа« System.Windows.Controls.Grid »».

Я знаю, что нет свойства шаблона для сетки, но как еще я могу переместить содержимое сетки в файл 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>

Что я пропустил?

1 Ответ

32 голосов
/ 26 января 2012

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, следуйте этомуссылка - http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx

...