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

时间:2013-05-09 07:29:00

标签: c# wpf dependency-properties routed-events

我需要举一个关于更改CurrentItem(称为UserControl)的DP(称为MenuButton)值的事件,以通知另一个UserControl更改其Title 1}} DP。一切正常,除了{em>旧值 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>

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

1 个答案:

答案 0 :(得分:0)

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