2012-02-25 4 views
3

ウィンドウにフォーカスが当たったときに何か起こりたいです。しかし、これは動作するようには思えません。私は交換した場合一方 EnterActionsが機能するように見えるときに、スタイルトリガーのセッターが機能しないのはなぜですか?

<Window x:Class="Sample.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300" Background="Black" 
     Opacity="0.5"> 
    <Grid> 

    </Grid> 
    <Window.Style> 
     <Style> 
      <Style.Triggers> 
       <Trigger Property="Window.IsActive" Value="true"> 
        <Setter Property="Control.Background" Value="Blue" /> 
        <Setter Property="Window.Title" Value="Testing" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Style> 

</Window> 

{Enter,Exit}Actionのものにアニメーションでセッターが動作するように見えます。

<Window x:Class="Sample.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300" Background="Black" 
     Opacity="0.5"> 
    <Grid> 

    </Grid> 
    <Window.Style> 
     <Style> 
      <Style.Triggers> 
       <Trigger Property="Window.IsActive" Value="true"> 
        <Trigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="Background.Color" 
               To="Blue" Duration="0:0:1" /> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Title"> 
            <DiscreteObjectKeyFrame Value="Testing" /> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
        </Trigger.EnterActions> 
        <Trigger.ExitActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="Background.Color" 
               To="Black" Duration="0:0:1" /> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Title"> 
            <DiscreteObjectKeyFrame Value="Window1" /> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
        </Trigger.ExitActions> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Style> 

</Window> 

私は、この回避策は、私の目的のためには十分であると仮定し、私は「シンプル」な方法が動作しない理由を理解したいと思います。

P.S.私は完全に動作する構文強調を得ることができません...それはいくつかのレベルのインデント後にあきらめているようです。

答えて

2

トリガーで何らかの条件に基づいて一部のプロパティ値を切り替えたい場合は、スタイル自体にこれらのプロパティのデフォルト値を設定する必要があります。それ以外の場合は、プロパティの値は常にオーバーライドされますローカル値は、これらのプロパティのうちdependency property value precedenceです。

<Window x:Class="Sample.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300" Opacity="0.5"> 
    <Grid> 
    </Grid> 
    <Window.Style> 
     <Style> 
      <Setter Property="Control.Background" Value="Black"/> 
      <Setter Property="Window.Title" Value="Window1" /> 
      <Style.Triggers> 
       <Trigger Property="Window.IsActive" Value="true"> 
        <Setter Property="Control.Background" Value="Blue" /> 
        <Setter Property="Window.Title" Value="Testing" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Style> 
</Window> 

また、あなたはトリガーがあなたのためにこれらの値を設定でき、設定値を省略することができます - あなたのケースでは、このようなあなたのスタイルにBackgroundTitleの値を設定する必要があります。これも機能します(ウィンドウ宣言時に背景とタイトルが省略されています)。 -

<Window x:Class="Sample.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300" Opacity="0.5"> 
    <Grid> 
    </Grid> 
    <Window.Style> 
     <Style> 
      <Style.Triggers> 
       <Trigger Property="Window.IsActive" Value="true"> 
        <Setter Property="Control.Background" Value="Blue" /> 
        <Setter Property="Window.Title" Value="Testing" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Style> 
</Window> 
+0

いいですね。私は、アニメーションが優先レベルでより高いので動作すると思います。私はあなたのリンクを読み上げます。 – seeker

関連する問題