我为我的问题找到了数百万个答案,但没有任何效果,也许我看不到简单的解决方案。
在我的MVVM-(Light)应用程序中,我想将Visibility属性绑定到ViewModel:
<StackPanel Orientation="Horizontal" Visibility="{Binding AddMenuVisibility, Converter={StaticResource BooleanToVisibilityConverter}}">
...
</StackPanel>
我的ViewModel始于:
public class OrderMainViewModel : BaseViewModel
{
public OrderMainViewModel()
{
if (CurrentUser.Default.UserLevel < 5)
{
AddMenuVisibility = false;
}
else
{
AddMenuVisibility = true;
}
}
属性:
public const string AddMenuVisibilityPropertyName = "AddMenuVisibility";
private bool _addMenuVisibility = true;
/// <summary>
/// Sets and gets the AddMenuVisibility property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public bool AddMenuVisibility
{
get
{
return _addMenuVisibility;
}
set
{
if (_addMenuVisibility == value)
{
return;
}
_addMenuVisibility = value;
RaisePropertyChanged(AddMenuVisibilityPropertyName);
}
}
CurrenUser.Default.UserLevel的默认值为1,因此它首先达到假值。后来它达到了真实值,但是XAML中的“真实”可见性状态没有改变。 如果我将第一个声明更改为true,则它保持可见。