【问题标题】:ControlTemplate not showing GridControlTemplate 不显示网格
【发布时间】:2012-05-22 22:47:02
【问题描述】:

我正在尝试创建一个包含网格的Button,其中的行包含某些文本。

Button 将有两行,两行都有不同的文本。显示的是传递给 ControlTemplate 的文本,但不是模板中指定的文本。

此外,网格行的高度没有显示。我希望它扩大按钮的高度。事实上,我不确定 Grid 是否真的在显示。

<Window x:Class="MAQButtonTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="695" Width="996">        
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
        </Style>
        <ControlTemplate TargetType="Control" x:Key="1">
            <Grid Width="444">
                <Grid.RowDefinitions>
                    <RowDefinition Height="51" />
                    <RowDefinition Height="36" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="0" Background="#286c97">
                    <TextBlock>This is the first piece of text</TextBlock>
                </Grid>
                <Grid Grid.Row="1" Background="#5898c0">
                    <ContentPresenter Grid.Row="0" />
                </Grid>
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" Background="#e9f1f6"></Grid>
        <Grid Grid.Column="1" Background="#d2e3ed">
            <StackPanel>
                <TextBlock FontFamily="Segoe UI" FontSize="22" FontWeight="Medium" Margin="52,58,0,0" Foreground="#0c3d5d">Your Quizzes <TextBlock FontFamily="Segoe UI" FontSize="18" FontWeight="Medium" Foreground="#0c3d5d">(7)</TextBlock></TextBlock>
                <Grid>
                    <Button Width="444" Background="{x:Null}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
                        <TextBlock>This is a second piece of text</TextBlock>
                    </Button>
                </Grid>
            </StackPanel>
        </Grid>

    </Grid>
</Window>

这是它目前的显示方式,并粗略地说明了我正在尝试实现的按钮布局:

【问题讨论】:

    标签: c# wpf xaml controltemplate


    【解决方案1】:

    当你应该设置模板时,你正在设置按钮的样式

    资源:

        <ControlTemplate TargetType="Button" x:Key="ButtonTemplate">
            <Grid Width="444">
                <Grid.RowDefinitions>
                    <RowDefinition Height="51" />
                    <RowDefinition Height="36" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="0" Background="#286c97">
                    <TextBlock>This is the first piece of text</TextBlock>
                </Grid>
                <Grid Grid.Row="1" Background="#5898c0">
                    <ContentPresenter />
                </Grid>
            </Grid>
        </ControlTemplate>
    

    还有按钮:

        <Button Template="{StaticResource ButtonTemplate}" >
            Text 2
        </Button>
    

    【讨论】:

      【解决方案2】:

      你为什么使用 TargetType 作为“控件”,使用 Button。您将需要定义一个按钮。使用 Blend 编辑按钮模板以去除其中所有不必要的内容。

      【讨论】:

      • 哎呀,我没有意识到我还在那里。我已将其更改为按钮,它仍然以完全相同的方式显示:/
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2014-08-23
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多