简体   繁体   中英

Can I identify which routed event from the triggers that caused the property change from code in WPF?

I have the below style trigger in the XAML, changing property based on a routed event which works properly.

        <Style.Triggers>
            <EventTrigger RoutedEvent="MouseDown">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource ThumbnailItemBackgroundSelected}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource ThumbnailItemBorderSelected}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource ThumbnailItemBackgroundMouseHover}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource ThumbnailItemBorderMouseHover}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>

I would like to get which event that caused the property change in OnPropertyChanged , but I didn't get the details about it in the override event.

 protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
 {
     if(e.Property.Name == "Background") {}
 }

Can I Identify which routed event (MouseDown or MouseEnter) that caused the property change (ex. Background) from code in any way?

You might not get MouseEnter and MouseDown events using OnPropertyChanged. You should use MouseEnter and MouseDown events respectively and call your code inside these Events respectively. Like this : Below Input => is a TextBox

private void Input_MouseEnter(object sender, MouseEventArgs e){
Input.Text = "Mouse Entered";}

private void Input_MouseLeave(object sender, MouseEventArgs e){
Input.Text = "Mouse Left";}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM