Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 为什么在向按钮添加加速器时样式会发生变化?_Wpf_Styles - Fatal编程技术网

Wpf 为什么在向按钮添加加速器时样式会发生变化?

Wpf 为什么在向按钮添加加速器时样式会发生变化?,wpf,styles,Wpf,Styles,当我将按钮的内容设置为普通字符串时,例如,则按钮的行为与普通字符串相同。但是如果我将内容更改为具有键盘加速器,例如,则按钮的样式将更改为具有不同的边距和大小 我有一个文本块样式,它没有键,所以被应用于所有文本块,我的问题是,为什么它在内容有加速器时应用,而在没有加速器时不应用 编辑:获取更多信息:默认样式位于按钮所在的StackPanel的资源中。我想问题真的是,当按钮有加速器时,为什么不应用默认的文本块样式?你应该用来验证这一点,但是会生成一个TextBlock来处理按钮内的文本。由于Text

当我将按钮的内容设置为普通字符串时,例如
,则按钮的行为与普通字符串相同。但是如果我将内容更改为具有键盘加速器,例如
,则按钮的样式将更改为具有不同的边距和大小

我有一个文本块样式,它没有键,所以被应用于所有文本块,我的问题是,为什么它在内容有加速器时应用,而在没有加速器时不应用


编辑:获取更多信息:默认样式位于按钮所在的StackPanel的资源中。我想问题真的是,当按钮有加速器时,为什么不应用默认的文本块样式?

你应该用
来验证这一点,但是
会生成一个
TextBlock
来处理按钮内的文本。由于TextBlock不支持快捷键,我敢打赌
会导致它生成一个
标签,而
标签将处理快捷键。

您应该用
验证这一点,但
会生成一个
TextBlock
来处理按钮内的文本。由于TextBlock不支持快捷键,我敢打赌
会导致它产生一个
标签,而
标签将负责快捷键。

WPF会在每个带有快捷键的按钮(和菜单)上添加一个TextBlock。
通过运行以下XAML可以看到这种效果(如果需要,记得挂接命令)

考虑到问题的范围,解决问题的关键是将TextAlignment设置为TextBlock的中心值。如果设置文本块样式的宽度(我的行在下面注释掉),文本将开始移动。添加HorizontalAlignment=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>

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

考虑到问题的范围,解决问题的关键是将TextAlignment设置为TextBlock的中心值。如果设置文本块样式的宽度(我的行在下面注释掉),文本将开始移动。添加HorizontalAlignment=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>


我想到了这一点,但事实似乎并非如此。虽然内容确实发生了变化,但它不是一个标签。没有加速器,它在AccessText中有一个TextBlock,没有加速器,它只是一个TextBlock。我想到了这一点,但情况似乎仍然不是这样。虽然内容确实发生了变化,但它不是一个标签。没有加速器,它在AccessText中有一个TextBlock,没有加速器,它只是一个TextBlock。