我需要在更改(被调用)的 DP(被调用)值时引发事件,以CurrentItem
通知另一个更改其DP。除了在目标 DP 中使用旧值之外,一切正常。如何强制它使用DP 的新值?UserControl
MenuButton
UserControl
Title
CurrentItem
CurrentItem
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:
我尝试使用 anEventTrigger
仅在 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>
太奇怪了!为什么这些触发器的行为与事件处理程序不同?