Wpf 强制属性将DP的回拨更改为火灾
我的问题听起来可能有点像 我有一个通知栏,当我的自定义UserControl的DependencyProperty发生更改时,它会经历某些动画。下面是代码实现:Wpf 强制属性将DP的回拨更改为火灾,wpf,mvvm,dependency-properties,change-notification,Wpf,Mvvm,Dependency Properties,Change Notification,我的问题听起来可能有点像 我有一个通知栏,当我的自定义UserControl的DependencyProperty发生更改时,它会经历某些动画。下面是代码实现: public string StatusBarText { get { return (string)GetValue(StatusBarTextProperty); } set { SetValue(StatusBarTextProperty, value); } } publ
public string StatusBarText
{
get { return (string)GetValue(StatusBarTextProperty); }
set { SetValue(StatusBarTextProperty, value); }
}
public static readonly DependencyProperty StatusBarTextProperty =
DependencyProperty.Register("StatusBarText", typeof(string), typeof(WorkspaceFrame), new FrameworkPropertyMetadata(null, StatusBarTextChangedCallBack));
private static void StatusBarTextChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(Convert.ToString(e.NewValue)))
{
var workspaceFrame = d as WorkspaceFrame;
if (null == workspaceFrame)
return;
var storyboard = workspaceFrame.FindResource("notificationBarAnimation") as Storyboard;
if (null == storyboard)
return;
var statusBar = workspaceFrame.Template.FindName("PART_StatusBar", workspaceFrame) as Border;
if (null == statusBar)
return;
//Run the animation
storyboard.Begin(statusBar);
}
}
这是正在设置动画的边界:
<Border x:Name="PART_StatusBar" Margin="5" BorderThickness="2" VerticalAlignment="Top"
DataContext="{Binding Path=StatusText}" Opacity="0"
Visibility="{Binding Path=Opacity, Mode=OneWay, RelativeSource={RelativeSource Self}, Converter={StaticResource doubleToVis}}"
BorderBrush="{StaticResource StatusMessageBackBrush}">
<Border.Background>
<SolidColorBrush Color="{StaticResource StatusMessageBackColor}" Opacity="0.7"/>
</Border.Background>
<TextBlock Margin="10" FontSize="17" Foreground="{StaticResource BlackColorBrush}" Text="{Binding}">
</TextBlock>
</Border>
现在您可能已经很清楚,此DP绑定到一个VM属性,该属性在设置时触发PropertyChangedNotification(INotifyProeprtyChanged的)。
现在的问题是,只有当DP的新旧值发生一些变化时,才会调用StatusBarTextChangedCallBack。有没有办法强迫它运行?如果没有,有什么办法吗?
我需要一遍又一遍地显示相同的通知。动画应该会启动
问候,
James您可以注册一个
强制evalueCallback
函数,而不是ValueChanged。大概是这样的:
public static readonly DependencyProperty StatusBarTextProperty =
DependencyProperty.Register("StatusBarText", typeof(string), typeof(WorkspaceFrame), new FrameworkPropertyMetadata(null,null, StatusBarTextCoerceValueCallBack));
private static object StatusBarTextCoerceValueCallBack(DependencyObject d, object value)
{
}
当属性的值更改时,即使值相同,也始终会触发强制值回调
谢谢实现所需功能(或手动调用事件处理程序)的正常方法是创建一个启动动画的方法。然后从属性更改处理程序内部调用该动画开始方法。这样,您就可以随时调用此方法。您的问题链接到另一个问题,该问题似乎提出了完全相同的问题。请说明为什么该问题及其答案不适用于您的情况。因为我不是通过代码直接设置DP。我正在把它装订好。如何从ViewModel在UserControl中进行方法调用?为什么不使用EventTriggers并在其中编写故事板?EventTriggers会侦听任何RoutedEvent,因此如果您想“强制”运行动画,只需编写自定义RoutedEvent并随时启动即可。EventTriggers完成其余的工作。之前已经完成了。但它有一个缺点。当视图和视图模型绑定且StatusBarText为空时,它也第一次启动了脚本。如果StatusBarText为null或空,我必须检查storyborad是否不应运行。当然,我可以在ViewModel级别自己做。但是第一次呢?@忍者等一下。你说写一个cutom路由事件。我以前在上运行过故事板。请你再解释一下,好让我尝试一下这种方法吗?我有这个想法。我不想采用这种方法,因为它不干净。但这似乎是最后的办法。在其他好办法出现之前,这似乎是唯一可行的解决办法。它不是完美的,而是有效的。谢谢@nitI我很抱歉我拿不到它。你能详细说明一下吗?我正在调用代码从PropertyChangedHandler内部设置动画。但是处理程序本身在一开始就不会被调用(直到e.NewValue中有一些更改)。