我在xmal中有一个StackLayout
属性,如下所示:
<StackLayout x:Name="_infoView"
Margin="0,10,0,10"
BackgroundColor="Black"
IsVisible="{Binding State}"/>
和ViewModel中的绑定bool变量
private Boolean _state = true;
public Boolean State
{
get { return _state; }
set { }
}
我的xmal中有一个按钮,想控制StackLayout的可见性,所以我做了类似的事情:
<Button x:Name="CloseButton"
Grid.Row="0"
Grid.Column="3"
Command="{Binding CloseWindowCommand}"/>
并在ViewModel中
CloseWindowCommand = new Command(CloseWindowTapped, CanCloseWindowTapped);
public ICommand CloseWindowCommand { get; set; }
public void CloseWindowTapped()
{
State = false;
}
public bool CanCloseWindowTapped()
{
return true;
}
我假设,通过点击CloseButton,我的StackLayout将消失......但它无法正常工作
答案 0 :(得分:2)
ViewModel应该实现INotifyPropertyChanged
接口,以通知View有关更改。
public class ViewModel : INotifyPropertyChanged
{
// Implementing INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName);
}
// In the setter of property raise event to inform view about changes
private Boolean _state = true;
public Boolean State
{
get
{
return _state;
}
set
{
_state = value;
RaisePropertyChanged();
}
}
}