这可能不像您想要的那么简单,但它可以满足您的需求。所有这些代码都在我的测试工具的主窗口中,因此可能需要更改某些内容以适应您的具体情况。这个想法是一样的。
我们需要做的第一件事是设置自己以在集合更改时获取事件。我使用了一个可观察的集合,因为如果你正在绑定,我会假设这就是你正在使用的:
ObservableCollection<String> m_NotificationList;
// Propertized for binding purposes
public ObservableCollection<String> NotificationList
{
get
{
return m_NotificationList;
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
m_NotificationList = new ObservableCollection<string>() { "hey", "ho", "lets", "go" };
m_NotificationList.CollectionChanged += CollectionChangeCallback;
}
下一步是定义一些路由事件,我们可以在需要闪烁时将其发送到窗口。我们在主窗口的类定义中这样做,如上:
public static readonly RoutedEvent FlashEvent =
EventManager.RegisterRoutedEvent("Flash",
RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(MainWindow));
public event RoutedEventHandler Flash
{
add { AddHandler(FlashEvent, value); }
remove { RemoveHandler(FlashEvent, value); }
}
现在我们需要在集合更新时定义回调,再次在主窗口的类定义中:
void CollectionChangeCallback(object sender, EventArgs e)
{
// Don't fire if we don't want to flash
if (m_NotificationList.Count > 0)
window.RaiseEvent(new RoutedEventArgs(FlashEvent));
}
现在我们转到 XAML 并向我们的 MainWindow 添加一个触发器,它处理我们刚刚创建的路由事件并执行我们想要执行的动画:
<Window.Triggers>
<EventTrigger RoutedEvent="local:MainWindow.Flash" >
<BeginStoryboard>
<Storyboard>
<ColorAnimation AutoReverse="True"
Duration="0:0:1"
FillBehavior="Stop"
From="White"
Storyboard.TargetName="window"
Storyboard.TargetProperty="Background.Color"
To="Red" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
这可以按照您在我的测试工具中的要求工作。这有点尴尬,但我找不到更好的方法来做到这一点。为清楚起见,我还将在此处包含带有代码隐藏的 XAML。它是一个空窗口,带有一个 MenuItem 和一个向集合添加字符串的按钮。您可以看到闪光以及事件如何结合在一起以实现它。
XAML:
<Window x:Class="WPFTestbed.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFTestbed"
x:Name="window"
Title="MainWindow"
Width="525"
Height="350">
<Window.Triggers>
<EventTrigger RoutedEvent="local:MainWindow.Flash" >
<BeginStoryboard>
<Storyboard>
<ColorAnimation AutoReverse="True"
Duration="0:0:1"
FillBehavior="Stop"
From="White"
Storyboard.TargetName="window"
Storyboard.TargetProperty="Background.Color"
To="Red" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
<StackPanel>
<MenuItem x:Name="menu"
Header="{Binding NotificationList.Count}"
HeaderStringFormat="Notifications ({0})"
ItemsSource="{Binding NotificationList}">
</MenuItem>
<Button Click="Button_Click"
Content="Hi" />
</StackPanel>
</Window>
后面的代码(-包括空间):
namespace WPFTestbed
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static readonly RoutedEvent FlashEvent =
EventManager.RegisterRoutedEvent("Flash",
RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(MainWindow));
public event RoutedEventHandler Flash
{
add { AddHandler(FlashEvent, value); }
remove { RemoveHandler(FlashEvent, value); }
}
ObservableCollection<String> m_NotificationList;
public ObservableCollection<String> NotificationList
{
get
{
return m_NotificationList;
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
m_NotificationList = new ObservableCollection<string>() { "hey", "ho", "lets", "go" };
m_NotificationList.CollectionChanged += CollectionChangeCallback;
}
void CollectionChangeCallback(object sender, EventArgs e)
{
if (m_NotificationList.Count > 0)
window.RaiseEvent(new RoutedEventArgs(FlashEvent));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
m_NotificationList.Add("Another");
}
}
}