我需要举一个关于更改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>
太奇怪了!为什么这些触发器的行为与事件处理程序不同?
答案 0 :(得分:0)
如果我没有忽略其他,那么原因可能是因为结构是通过值传递的。这个原因有两个副本。一个由你的绑定使用,另一个是原始绑定。这意味着,如果绑定更新内存中的结构而不是原始结构。