1

我正在尝试在xaml. 进展顺利,但我在内容对齐方面遇到了一些奇怪的问题。

如果我在 中放置一个自定义按钮"root grid",则整个按钮都是可点击的,并且内容(按钮的文本)正确居中对齐。

但是,如果我以这种方式放置自定义按钮:

  • 边界
    • 堆栈面板
      • 我的按钮
      • 我的按钮
      • ETC...

然后,我的按钮 - 内容也正确居中,但只能点击内容的文本。不是整个按钮...

如果我将我的内容对齐设置为拉伸,它是可点击的,但文本不是中心。

这是我的 xaml:如何在上述设置中使整个按钮可点击?

控制模板:

<ControlTemplate x:Key="ollema">
    <Grid Background="{TemplateBinding Background}" VerticalAlignment="Stretch">
        <Border Name="border" 
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}">
            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                <ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                </ContentPresenter>
            </StackPanel>
        </Border>
    </Grid>
</ControlTemplate>

我的主窗口中的 XAML:

<Border BorderBrush="White" BorderThickness="1" Margin="0" Width="200" Background="#FF00641E" HorizontalAlignment="Center" VerticalAlignment="Center">
    <StackPanel Width="150" HorizontalAlignment="Center" VerticalAlignment="Center">
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="{x:Null}" Foreground="#FFE8E8E8" Background="{x:Null}" BorderThickness="0"/>
    </StackPanel>
</Border>
4

2 回答 2

0

你能试着把

VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"

对于 stackpanel 和 contentPresenter

于 2013-05-03T12:18:25.150 回答
0

@DeMama 将您BackgroundToggleButton's 设置Transparentx:Null

就像是

<Border BorderBrush="White" BorderThickness="1" Margin="0" Width="200" Background="#FF00641E" HorizontalAlignment="Center" VerticalAlignment="Center">
    <StackPanel Width="150" HorizontalAlignment="Center" VerticalAlignment="Center">
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="White" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0,0,0,1"/>
            <ToggleButton Template="{StaticResource ollema}" Content="ToggleButton" Height="57" BorderBrush="{x:Null}" Foreground="#FFE8E8E8" Background="Transparent" BorderThickness="0"/>
    </StackPanel>
</Border>

刚试过这个,即使在外面也可以很好地调用点击事件ContentPresenter

于 2013-05-03T13:57:33.373 回答