2011-08-03 14 views
2

コントロール内の個々のコンポーネントにイベントがあるかどうかは疑問です。たとえば、私自身のコントロールを作成しました.VisualStateManagerを使用すると、コントロール全体で発生するいくつかのイベントを処理できます。私はmouseoverイベントを発生させるためにコントロール内のトグルボタンを使用したいと思います。現在のところ、マウスが上にあるコントロールのどの領域でもマウスオーバーが発生し、取得しようとしているカラーアニメーションの効果が失われています。トグルボタンがマウスオーバーしたときにのみカラーアニメーションを行い、コンテンツ、私はそのピースがイベントを発生させたくありません。コントロール内の別のイベント

私がgeneric.xamlファイルの下で話しているものの例。

<Style TargetType="local:MyControl"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:MyControl"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="ViewStates"> 

          <VisualState x:Name="Expanded"> 
           <Storyboard> 
            <DoubleAnimation Storyboard.TargetName="ContentScaleTransform" Storyboard.TargetProperty="ScaleY" To="1" Duration="0"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Collapsed"> 
           <Storyboard> 
            <DoubleAnimation Storyboard.TargetName="ContentScaleTransform" Storyboard.TargetProperty="ScaleY" To="0" Duration="0"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <!-- This fires for any part of the control, is it possible to just create a mouseover for the ToggleButton below? --> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}" Background="{TemplateBinding Background}"> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto"/> 
           <RowDefinition Height="Auto"/> 
          </Grid.RowDefinitions> 
          <Grid Margin="3"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="*"/> 
           </Grid.ColumnDefinitions> 
           <ToggleButton Grid.Column="0" Content="{TemplateBinding HeaderContent}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" Margin="1" x:Name="ExpandCollapseButton"> 

           </ToggleButton> 
          </Grid> 
          <ContentPresenter Grid.Row="1" Margin="5" Content="{TemplateBinding Content}" x:Name="Content"> 
           <ContentPresenter.RenderTransform> 
            <ScaleTransform x:Name="ContentScaleTransform"/> 
           </ContentPresenter.RenderTransform> 
          </ContentPresenter> 
         </Grid> 
        </Border> 

       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

答えて

2

ボブ、あなたはこのようなあなたのクラスからトグルボタンにアクセスすることができます。トグルボタンとして

トグルボタンmyToggleButton = GetTemplateChild(「トグルボタン」);

クラスに必要なイベントを作成します。 OnApplyTemplateメソッドをオーバーライドしてそこにイベントを作成する必要があるかもしれませんが、私はまだこの部分を研究しており、まだ完全に理解していないので、あなたはそれを再生する必要があります。私はOnApplyTemplateをオーバーライドしてくれました。

[TemplateVisualState(Name = "MouseEnter", GroupName = "ViewStates")] 

visualSTATEのは、あなたの他のvisualstatesを設定しているとである必要はありませんgeneric.xamlファイルに存在することができます

ToggleButton myToggleButton; 
public override void OnApplyTemplate() { 
    base.OnApplyTemplate(); 
    myToggleButton = GetTemplateChild("toggleButton") as ToggleButton; 
    myToggleButton.MouseEnter += new MouseEventHandler(myToggleButton_MouseEnter); 
} 
void myToggleButton_MouseEnter(object sender, MouseEventArgs e) { 
     VisualStateManager.GoToState(this, "MouseEnter", true); 
    } 

また上部に設定テンプレートに確認してください内側のToggleButton

<VisualState x:Name="MouseEnter"> 
    <Storyboard> 
     <ColorAnimation Storyboard.TargetName="toggleButton" Storyboard.TargetProperty="(ToggleButton.SomeProperty).(SolidColorBrush.Color)" To="SomeColor"/> 
    </Storyboard> 
</VisualState> 
+0

これはうまくいきました。ありがとうございます。 – Terco

1

まず第一に、あなたは「視覚的な状態」について話している「イベント」について話されていない、私たちの専門用語が正しい取得することができます。 「MouseOver」は視覚的な状態です。コントロール内のコードの責任は、コントロールのビジュアル状態を決定することです。

通常、コントロール内のコードはMouseEnterイベントとMouseLeaveイベントを使用して、マウスがコントロール上にあることを示すブール値を設定します。いくつかのUpdateStatesメソッドを呼び出します。このメソッドでは、コントロールが現時点でどの視覚状態にすべきかを決定するロジックが開発者に含まれます。

MouseEnterMouseLeaveのコントロールを使用するのではなく、これらのハンドラを内部のToggleButtonに接続します。

+0

ビジュアルステートを内側のトグルボタンに追加することを意味しますか? – Terco

+0

@Bob:いいえ、コードを書いてそのコードをMouseEnterにイベントハンドラとして、ToggleButtonのMouseLeave __events__にアタッチする必要があるということです。このコードは 'VisualStateManager.GotoState'を使用して、コントロールがどのような視覚状態にあるのかを指定します。この混乱がどのように発生しているのか、' MyControl'がどのような基本クラスから派生しているのでしょうか? – AnthonyWJones

関連する問題