2

コントロール内の個々のコンポーネントにイベントを持たせることができるかどうか疑問に思っています。たとえば、独自のコントロールを作成しました。VisualStateManagerを使用すると、コントロール全体で発生するいくつかのイベントを処理できます。コントロールのトグルボタンだけで、マウスオーバーイベントを発生させることができます。現在、マウスが置かれているコントロールの任意の領域でマウスオーバーが発生し、取得しようとしているcoloranimation効果が台無しになります。トグルボタンにマウスが置かれている場合にのみcoloranimationが発生するようにしたいのですが、コントロールには他のコンテンツが表示されます。コンテンツ、私はその作品がイベントを発火させたくありません。

以下で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>
4

2 に答える 2

2

ボブ、次のようにクラスからトグルボタンにアクセスできます。

ToggleButton myToggleButton = GetTemplateChild( "toggleButton")as ToggleButton;

クラスで必要なイベントを作成します。OnApplyTemplateメソッドをオーバーライドして、そこでイベントを作成する必要がある場合があります。私はまだこの部分を調査中であり、まだ完全には理解していません。試してみる必要があります。OnApplyTemplateをオーバーライドしましたが、うまくいきました。

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);
    }

また、テンプレートを上部に設定してください。

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

VisualStateは、他のvisualstatesを設定しているgeneric.xamlファイルに存在でき、内部のToggleButtonに存在する必要はありません。

<VisualState x:Name="MouseEnter">
    <Storyboard>
        <ColorAnimation Storyboard.TargetName="toggleButton" Storyboard.TargetProperty="(ToggleButton.SomeProperty).(SolidColorBrush.Color)" To="SomeColor"/>
    </Storyboard>
</VisualState>
于 2011-08-04T19:29:06.587 に答える
1

まず第一に、私たちの用語を正しくしましょう。あなたは「視覚的状態」について話している「イベント」について話しているのではありません。「MouseOver」は視覚的な状態です。コントロールがどの視覚状態にあるかを決定するためのコントロール内のコードの責任。

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

あなたの場合、コントロールMouseEnterMouseLeaveイベントを使用するのではなく、代わりにこれらのハンドラーを内部にアタッチしますToggleButton

于 2011-08-03T20:48:51.623 に答える