【发布时间】:2021-09-29 07:29:21
【问题描述】:
我正在尝试做类似于Binding to static class property 的事情。我想将多个控件的 IsVisible 属性绑定到单个静态布尔值(这样我就可以使用单个 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 事件的引发”有关,但我不知道该怎么办。
【问题讨论】:
-
只能绑定公共属性,不能绑定字段
-
public bool IsChangingPassword { get; set; }= true; -
并且,如果你想在运行时动态更新你需要实现 INotifyPropertyChanged
-
与字段(而不是属性)的静态绑定工作得很好(除了关系是“一次性的”)。
标签: c# xamarin.forms binding