为什么我的RoutedEvent使用旧的DependencyProperty值?

本文关键字:DependencyProperty 我的 RoutedEvent 为什么 | 更新日期: 2023-09-27 18:13:01

我需要在更改UserControl(称为MenuButton)的DP(称为CurrentItem)值时引发事件,以通知另一个UserControl更改其Title DP。除了CurrentItem旧值在目标DP中使用之外,一切都很好。我怎么能强迫它使用CurrentItem DP的新值?


public partial class MenuButton : UserControl
{
    ...
    public struct ZoneMenuItem
    {
        public string MenuTitle
        {
            get;
            set;
        }
        public string ZoneTitle
        {
            get;
            set;
        }
        public ZoneMenuItem(string menuTitle, string zoneTitle)
        {
            this = new ZoneMenuItem();
            this.MenuTitle = menuTitle;
            this.ZoneTitle = zoneTitle;
        }
    }
    #region ________________________________________  CurrentItemChanged RoutedEvent
    public event RoutedEventHandler CurrentItemChanged
    {
        add { AddHandler(CurrentItemChangedEvent, value); }
        remove { RemoveHandler(CurrentItemChangedEvent, value); }
    }
    public static readonly RoutedEvent CurrentItemChangedEvent =
        EventManager.RegisterRoutedEvent("CurrentItemChanged", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(MenuButton));
    #endregion
    #region ________________________________________  CurrentItem DependencyProperty
    public ZoneMenuItem CurrentItem
    {
        get { return (ZoneMenuItem)GetValue(CurrentItemProperty); }
        set { SetValue(CurrentItemProperty, value); }
    }
    public static readonly DependencyProperty CurrentItemProperty =
        DependencyProperty.Register("CurrentItem",
                                    typeof(ZoneMenuItem),
                                    typeof(MenuButton),
                                    new FrameworkPropertyMetadata(new ZoneMenuItem(), FrameworkPropertyMetadataOptions.None, OnCurrentItemPropertyChanged));
    private static void OnCurrentItemPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        MenuButton instance = sender as MenuButton;
        if (instance != null)
        {
            instance.RaiseEvent(new RoutedEventArgs(MenuButton.CurrentItemChangedEvent));
        }
    }
    #endregion
}
如上面所示,我在CurrentItem属性更改时引发了一个事件。它在另一个UserControl中使用,像这样:
<i:EventTrigger SourceName="menuButton" EventName="CurrentItemChanged">
    <ei:ChangePropertyAction TargetObject="{Binding}" PropertyName="Title">
        <ei:ChangePropertyAction.Value>
            <Binding ElementName="menuButton" Path="CurrentItem.ZoneTitle"/>
        </ei:ChangePropertyAction.Value>
    </ei:ChangePropertyAction>
</i:EventTrigger>

,

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

更新1:

我找到了一个解决方法,通过使用代码隐藏而不是利用交互触发器。(为什么它可以工作?!)

menuButton.CurrentItemChanged += menuButton_CurrentItemChanged;

private void menuButton_CurrentItemChanged(object sender, RoutedEventArgs e)
{
    this.Title = (sender as MenuButton).CurrentItem.ZoneTitle;
}

更新2:

我试图使用EventTrigger只在XAML中执行所有任务,但它的行为与交互触发器完全相同!

<vc:MenuButton x:Name="menuButton" VerticalAlignment="Center" Margin="3,3,0,0">
    <vc:MenuButton.Triggers>
        <EventTrigger SourceName="menuButton" RoutedEvent="vc:MenuButton.CurrentItemChanged">
            <BeginStoryboard>
                <Storyboard>
                    <StringAnimationUsingKeyFrames Storyboard.Target="{Binding}" Storyboard.TargetProperty="Title">
                        <DiscreteStringKeyFrame KeyTime="0" Value="{Binding ElementName=menuButton, Path=CurrentItem.ZoneTitle}"/>
                    </StringAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </vc:MenuButton.Triggers>
</vc:MenuButton>

太奇怪了!为什么这些触发器具有与事件处理程序不同的行为?

为什么我的RoutedEvent使用旧的DependencyProperty值?

如果我没有忽略其他,那么原因可能是因为结构体是按值传递的。这个原因有两个副本。一个是绑定使用的另一个是原始的。这意味着,如果你的绑定更新的结构体是在内存中,而不是原来的。

相关文章:
  • 没有找到相关文章