1

私はこれの初心者であり、WPFとXAMLがどのように機能するかを理解しようとしています。次のスニペットは、Nathans Unleashed 4.0の本からの(些細な変更)です。[OK]ボタンに挿入しました:

<Button.Style>
 <Style TargetType=”{x:Type Button}”&gt;
  <Style.Triggers>
   <Trigger Property=”IsMouseOver” Value=”True”&gt;
    <Setter Property=”Background” Value=”Yellow”/>
   </Trigger>
  </Style.Triggers>
 </Style>
</Button.Style>

これをXAMLクランシャーで実行し、マウスを[OK]ボタンの上に移動すると、ボタンは背景色を黄色(細かい)に変更しますが、マウスがボタンの上にある場合でも、すぐに元の値にリセットされます-理由これは?マウスがボタンから離れるまで黄色のままになると思います。これはXAMLクランシャーの問題ですか、それとも私の期待は間違っていますか?

編集(コメントへの応答):これは、ネイサンの本から取られた完全なウィンドウです:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
    Background="OrangeRed">
  <StackPanel>
   <Label FontWeight="Bold" FontSize="20" Foreground="White">
    WPF Unleashed (Version 3.0)
   </Label>
   <Label>© 2006 SAMS Publishing</Label>
   <Label>Installed Chapters:</Label>
   <ListBox>
    <ListBoxItem>Chapter 1</ListBoxItem>
    <ListBoxItem>Chapter 2</ListBoxItem>
   </ListBox>
   <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
   <Button MinWidth="75" Margin="10">Help</Button>
   <Button MinWidth="75" Margin="10">
    <Button.Style>
     <Style TargetType="{x:Type Button}">
      <Style.Triggers>
       <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Background" Value="Yellow"/>
       </Trigger>
      </Style.Triggers>
     </Style>
     </Button.Style>
     OK
     </Button>
    </StackPanel>
   <StatusBar>You have successfully registered this product.</StatusBar>
  </StackPanel>
</Window>
4

2 に答える 2

3

残念ながら、アニメーションの上に凝ったホバーがボタンに組み込まれているため、これが発生しないようにするには、ControlTemplateをオーバーライドする必要があります。

 <Button MinWidth="75" Margin="10" FocusVisualStyle="{x:Null}" Content="OK">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Name="border" BorderThickness="1" Padding="4,2" BorderBrush="DarkGray" CornerRadius="3" Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Yellow"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
于 2012-12-13T21:05:09.360 に答える
0

コードを新しいVisualStudio2010プロジェクトにコピーして、正常に実行しました(.NET4.0)。

問題はXAMLCruncherのバグだと思います。

于 2012-12-13T16:25:13.327 に答える