为什么我看不到“Visible”属性的绑定?

问题描述 投票:0回答:2
c# wpf xaml binding
2个回答
1
投票

我希望我正确理解了这个问题,您与

Res_Visible
属性进行了绑定,我怀疑您想更改可见性,但是当您从
CassetteLevelViewModel
更改它时,它不起作用。

要通知

View
(Xaml) 您的情况下
ViewModel
中的属性 (
CassetteLevelViewModel
) 已更改值,您应该在
INotifyPropertyChanged
中使用
BaseViewModel

如果您不使用,您可以在下面找到您应该添加的内容

BaseViewModel

public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

OnPropertyChanged()
方法通知View属性值已经改变。

如果您已经使用,您只需要修改您的属性,如下例所示。

private Visibility _res_Visibile;
public Visibility Res_Visibile
    {
        get => _res_Visibile;
        set
        {
            _res_Visibile = value;
            OnPropertyChanged();
        }
    }

-1
投票

经过相当多的实验,我在属性的定义中找到了解决方案:
我变了:

public Visibility Res_Vis {get;set;}

进入:

public Visibility Res_Vis {get;set;} = Visibility.Hidden;

现在一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.