0

私はWPFを学んでいます。私は適用しようとしBackgroundForegroundいて、TextBlock使用していましたStyle.Trigger。私が定義Triggerした から、フォアグラウンドが で変更されていることに気付くことができますMouseOverが、 ではありませんBackground。助けてください。以下は私のXAMLです:

<Window x:Class="WpfApplication1.ApplyingStyle"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ApplyingStyle"
        Height="300"
        Width="300">
  <Window.Resources>
    <Style x:Key="myStyle">
      <Setter Property="Control.Background"
              Value="Red" />
      <Setter Property="Control.FontFamily"
              Value="Times New Roman" />
      <Setter Property="Control.FontSize"
              Value="25" />
      <Style.Triggers>
        <Trigger Property="Control.IsMouseOver"
                 Value="True">
          <Setter Property="Control.Foreground"
                  Value="HotPink" />
          <Setter Property="Control.Background"
                  Value="Blue" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </Window.Resources>
  <Grid ShowGridLines="True">
    <Grid.RowDefinitions>
      <RowDefinition   Height="*" />
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
    <TextBlock Name="myTextBlock"
               Grid.Row="1"
               Grid.Column="0"
               Style="{StaticResource myStyle}">Hello</TextBlock>
  </Grid>
</Window>
4

2 に答える 2

1

を使用することをお勧めしStyle.TargetTypeます。これにより、

  1. Setter.Property値を短くする
  2. 通常のプロパティと添付プロパティの間のあいまいさを防ぐ
  3. ターゲット タイプが実際にそのプロパティを持っている場合は、IDE 検証を取得します。

TextBlockではないため、Controlこれは機能しません。を使用TargetType="TextBlock"して に変更Control.BackgroundするBackgroundか、 を使用しますTextBlock.Background

(スタイルを操作するときは、値の優先順位にも注意してください。 を設定するBackgroundと、スタイルを完全に上書きTextBlockするローカル値が得られます)

于 2013-02-27T03:03:51.627 に答える
0

Control.BackgrounduseTextBlock.Backgroundまたは単に to を設定TargetTypeする代わりに、スタイル宣言でスタイルを適用しようとしているコントロール Type を使用してみてくださいTextBlock。あなたのスタイルをこのようなものに設定するとうまくいくようです。

<Window x:Class="WpfApplication1.ApplyingStyle"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ApplyingStyle" Height="300" Width="300">
    <Window.Resources>
        <Style x:Key="myStyle" TargetType="TextBlock">
            <Setter Property="Background" Value="Red" />
            <Setter Property="FontFamily" Value="Times New Roman" />
            <Setter Property="FontSize" Value="25" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True"  >
                    <Setter Property="Background" Value="Blue" />
                    <Setter Property="Foreground" Value="HotPink" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid ShowGridLines="True" >
        <Grid.RowDefinitions>
            <RowDefinition   Height="*" />
            <RowDefinition/>
            <RowDefinition />
        </Grid.RowDefinitions>

        <TextBlock Name="myTextBlock"  
                   Grid.Row="1" 
                   Grid.Column="0" 
                   Style="{StaticResource myStyle}">Hello</TextBlock>
    </Grid>
</Window>
于 2013-02-27T02:58:52.823 に答える