我有一个静态属性,在静态类中,我绑定到一个框架的Source属性。这是一个OneWay绑定。绑定在第一次正确工作但在属性更改时不更新目标。这是我的xaml
<Frame x:Name="frmMain" Source="{Binding Source={x:Static currentPage:ActivePages.MainFramePage}, NotifyOnTargetUpdated=True,Mode=OneWay}"/>
这是我的静态类ActivePages.cs
我关注了this link和another link 当源更改时,如何更新它?public static class ActivePages { private static Uri mainFramePage; public static Uri MainFramePage { get { return mainFramePage; } set { mainFramePage = value; MainFramePageChanged?.Invoke(null, new PropertyChangedEventArgs("MainFramePage")); } } public static event EventHandler<PropertyChangedEventArgs> MainFramePageChanged; }
答案 0 :(得分:0)
如果它是WPF 4.5或更高版本,您可以使用Path属性:
<Frame x:Name="frmMain" Source="{Binding Path=(local:ActivePages.MainFramePage), NotifyOnTargetUpdated=True,Mode=OneWay}"/>
答案 1 :(得分:0)
根据MSDN page,静态属性更改事件的签名是
public static event EventHandler MyPropertyChanged;
或
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
所以你应该
public static event EventHandler MainFramePageChanged;
而不是
public static event EventHandler<PropertyChangedEventArgs> MainFramePageChanged;
但是,如果您有多个静态属性,则最好使用单个事件
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
,PropertyChangedEventArgs
提供属性名称信息。
您还应该将此语法用于Binding表达式:
Source="{Binding Path=(currentPage:ActivePages.MainFramePage)}"
有关静态属性更改通知的详细信息,另请参阅this blog post。