【发布时间】:2018-04-21 14:35:46
【问题描述】:
我希望按钮在 State.Away 和 State.Stop 中可见,但由于某种原因,即使 State 与 State.Away 和 State.Stop 不同,该按钮也始终可见。
Xaml:
<Button Text="Hello" IsVisible="{Binding View}"/>
视图模型:
private bool myBool;
public bool View
{
get
{
if (State == State.Away || State == State.Gone)
{
myBool = true;
}
else
{
myBool = false;
}
return myBool;
}
}
【问题讨论】:
-
您的虚拟机需要实现 INotifyPropertyChanged 并在 View 的值发生变化时引发 PropertyChanged 事件
-
如果总是返回false,按钮是否不可见?那么绑定可能不知道当“状态”改变时“视图”改变了。
-
另外,请确保控件的 DataContext 正确设置为您的 ViewModel
-
我会考虑使用
IValueConverter将IsVisible直接绑定到State,该IValueConverter知道如何将状态映射到您想要的可见性。这样您就不必担心在多个属性上引发PropertyChanged,并且可以完全摆脱View,从而简化您的代码。
标签: c# xamarin mvvm xamarin.forms xamarin-studio