代码之家  ›  专栏  ›  技术社区  ›  Ray

为什么在按钮上添加加速器时样式会改变?

  •  1
  • Ray  · 技术社区  · 14 年前

    <Button Content="Ok" /> 然后按钮的行为正常。但如果我将内容更改为使用键盘加速器,例如。 <Button Content="_Ok" /> 按钮的样式更改为具有不同的边距和大小。

    编辑: 为什么当按钮有加速器时不应用默认的文本块样式?

    2 回复  |  直到 13 年前
        1
  •  1
  •   Zamboni    14 年前

    WPF使用加速器向每个按钮(和菜单)添加一个文本块。
    通过运行下面的XAML可以看到这种效果(如果需要,请记住连接命令)。

    考虑到问题的范围,解决问题的关键是将文本设置为文本块的中心值。如果设置文本块样式的宽度(下面注释掉我的行),文本将开始移动。添加horizontallagnment=Center也有助于使文本块/按钮居中,但这也会影响其他文本块控件。

    <Window x:Class="ButtonAccelerator.Views.MainView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Main Window" Height="400" Width="800">
      <Window.Resources>
        <Style TargetType="TextBlock">
            <!--<Setter Property="Width" Value="70"/>-->
            <Setter Property="Height" Value="23"/>
            <Setter Property="Background" Value="Pink"/>
            <Setter Property="TextAlignment" Value="Center"/>
        </Style>
        <Style TargetType="Button">
            <Setter Property="Width" Value="70"/>
            <Setter Property="Height" Value="23"/>
        </Style>
      </Window.Resources>
      <DockPanel>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <StackPanel 
                Grid.Row="1" Grid.Column="1"
                HorizontalAlignment="Right"
                Orientation="Horizontal">
                <TextBlock Text="OK" />
                <Button 
                    Content="OK"/>
                <Button 
                    Content="_OK"/>
            </StackPanel>
        </Grid>
      </DockPanel>
    </Window>
    
        2
  •  0
  •   Bryan Anderson    14 年前

    你应该用 Snoop 但是 <Button Content="Ok" /> TextBlock 处理按钮内的文本。因为TextBlock不支持快捷键,我敢打赌 <Button Content="_Ok" /> 使它产生 Label 而是因为 会处理好油门钥匙的。