将 Window.IsEnabled 绑定到静态布尔值 属性
Binding Window.IsEnabled to static bool property
我的 WPF 项目中有这个 App.xaml.cs
代码:
public partial class App : Application
{
public static bool IsInitialized
{
get;
private set;
}
public static async Task Initialize()
{
// Mark application as initialized
IsInitialized = true;
}
}
我的应用程序的主要window应该被禁用(IsEnabled
== False
)而App.IsInitialized
标志未设置,因此 window 在 Initialize()
完成后启用。
如何实现?
尝试使用此 XAML:
IsEnabled="{Binding App.IsInitialized, Mode=TwoWay}"
取自(并修改)documentation 中的 MS 示例:
<Binding Source="{x:Static Application.Current}" Path="Initialized"/>
您可以使用:
IsEnabled="{Binding Source={x:Static Application.Current}, Path=Initialized}"
并且您还应该在 属性 Initialized 更新时通知,以便 UI 也更新,为此您应该实现 INotifyPropertyChanged 接口并在 Initialize 上引发 PropertyChange 事件() 方法。
希望这有帮助。
是的,静态在大多数情况下是不正确的,所以我要实施 INotifyPropertyChanged,这样 UI 就会收到来自我的 'controller' class 的更新通知。
此外,这是未来的绝妙代码:https://gist.github.com/schuster-rainer/2644730
这是 INotifyPropertyChanged 实现示例。
public class AppController : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool m_bInit;
private PropertyChangedEventArgs m_bInitEA = new PropertyChangedEventArgs("IsInitialized");
public bool IsInitialized
{
get { return m_bInit; }
set
{
m_bInit = value;
if (PropertyChanged != null)
PropertyChanged(this, m_bInitEA);
}
}
}
这是XAML:
<Window x:Class=".......
Loaded="OnLoaded" DataContext="{x:Static Application.Current}"
IsEnabled="{Binding Controller.IsInitialized}">
我的 WPF 项目中有这个 App.xaml.cs
代码:
public partial class App : Application
{
public static bool IsInitialized
{
get;
private set;
}
public static async Task Initialize()
{
// Mark application as initialized
IsInitialized = true;
}
}
我的应用程序的主要window应该被禁用(IsEnabled
== False
)而App.IsInitialized
标志未设置,因此 window 在 Initialize()
完成后启用。
如何实现?
尝试使用此 XAML:
IsEnabled="{Binding App.IsInitialized, Mode=TwoWay}"
取自(并修改)documentation 中的 MS 示例:
<Binding Source="{x:Static Application.Current}" Path="Initialized"/>
您可以使用:
IsEnabled="{Binding Source={x:Static Application.Current}, Path=Initialized}"
并且您还应该在 属性 Initialized 更新时通知,以便 UI 也更新,为此您应该实现 INotifyPropertyChanged 接口并在 Initialize 上引发 PropertyChange 事件() 方法。
希望这有帮助。
是的,静态在大多数情况下是不正确的,所以我要实施 INotifyPropertyChanged,这样 UI 就会收到来自我的 'controller' class 的更新通知。
此外,这是未来的绝妙代码:https://gist.github.com/schuster-rainer/2644730
这是 INotifyPropertyChanged 实现示例。
public class AppController : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool m_bInit;
private PropertyChangedEventArgs m_bInitEA = new PropertyChangedEventArgs("IsInitialized");
public bool IsInitialized
{
get { return m_bInit; }
set
{
m_bInit = value;
if (PropertyChanged != null)
PropertyChanged(this, m_bInitEA);
}
}
}
这是XAML:
<Window x:Class=".......
Loaded="OnLoaded" DataContext="{x:Static Application.Current}"
IsEnabled="{Binding Controller.IsInitialized}">