无法成功地将 IsVisible 动态绑定到静态字段
Unable to successfully dynamically bind IsVisible to static field
我正在尝试做类似于 Binding to static class property 的事情。我想将多个控件的 IsVisible 属性 绑定到单个静态 bool (这样我就可以让它们全部出现和消失单个 C# 语句)。
这是我的 XAML 控件之一:
<Label Grid.Row="3"
x:Name="LabelDireWarning"
Grid.ColumnSpan="2"
TextColor="Red"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
IsVisible="{Binding Source={x:Static local:State.IsChangingPassword}}"
Text="blah blah."/>
这是字段:
public static class State
{
public static bool IsChangingPassword = true;
etc.
我有一个切换 IsChangingPassword
的测试按钮,但控件的可见性没有改变。
我猜这与“PropertyChanged
事件的引发”有关,但我该怎么办?
这是 WPF 4.5 中支持绑定到静态属性的新功能之一。它可能不适用于 Xamarin.Forms
。
和一样,如果你想在运行时间动态更新,你需要实现INotifyPropertyChanged
。但是在表单中,static class 无法实现接口。
所以你应该做一些改变:
public static class State
{
private static Status g_Current = new Status();
public static Status Current
{
get
{
return g_Current;
}
}
public class Status : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _isChangingPassword = true;
public bool IsChangingPassword
{
get { return _isChangingPassword; }
set
{
if (value != _isChangingPassword)
{
_isChangingPassword = value;
NotifyPropertyChanged("IsChangingPassword");
}
}
}
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
在你的 XAML:
<Label Grid.Row="3"
x:Name="LabelDireWarning"
Grid.ColumnSpan="2"
TextColor="Red"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
IsVisible="{Binding Source={x:Static local:State.Current}, Path=IsChangingPassword}"
Text="blah blah."/>
然后当您可以更改代码隐藏中的 IsChangingPassword
时,例如:
State.Current.IsChangingPassword = false;
我正在尝试做类似于 Binding to static class property 的事情。我想将多个控件的 IsVisible 属性 绑定到单个静态 bool (这样我就可以让它们全部出现和消失单个 C# 语句)。
这是我的 XAML 控件之一:
<Label Grid.Row="3"
x:Name="LabelDireWarning"
Grid.ColumnSpan="2"
TextColor="Red"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
IsVisible="{Binding Source={x:Static local:State.IsChangingPassword}}"
Text="blah blah."/>
这是字段:
public static class State
{
public static bool IsChangingPassword = true;
etc.
我有一个切换 IsChangingPassword
的测试按钮,但控件的可见性没有改变。
我猜这与“PropertyChanged
事件的引发”有关,但我该怎么办?
这是 WPF 4.5 中支持绑定到静态属性的新功能之一。它可能不适用于 Xamarin.Forms
。
和INotifyPropertyChanged
。但是在表单中,static class 无法实现接口。
所以你应该做一些改变:
public static class State
{
private static Status g_Current = new Status();
public static Status Current
{
get
{
return g_Current;
}
}
public class Status : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _isChangingPassword = true;
public bool IsChangingPassword
{
get { return _isChangingPassword; }
set
{
if (value != _isChangingPassword)
{
_isChangingPassword = value;
NotifyPropertyChanged("IsChangingPassword");
}
}
}
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
在你的 XAML:
<Label Grid.Row="3"
x:Name="LabelDireWarning"
Grid.ColumnSpan="2"
TextColor="Red"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
IsVisible="{Binding Source={x:Static local:State.Current}, Path=IsChangingPassword}"
Text="blah blah."/>
然后当您可以更改代码隐藏中的 IsChangingPassword
时,例如:
State.Current.IsChangingPassword = false;