如何在XAML中设置标签的可见性,以便在该标签的Text属性不为null时变为可见?

Ala*_*an2 0 c# xaml xamarin xamarin.forms

如何在XAML中设置标签的可见性,以便在该标签的Text属性不为null时变为可见?

我有这个代码:

<Label Grid.Row="0" Text="*" IsVisible="emptyLabel1 == null"/>
<Label Grid.Row="0" Text="{Binding EmptyLabel1}" IsVisible="emptyLabel1 == null"/>
<Label Grid.Row="1" Text="*" IsVisible="emptyLabel2 == null"/>
<Label Grid.Row="1" Text="{Binding EmptyLabel2}" IsVisible="emptyLabel2 == null"/>
Run Code Online (Sandbox Code Playgroud)

在我的VM中它看起来像这样:

    private string emptyLabel1;
    private string emptyLabel2;
    public string EmptyLabel1
    {
        get { return emptyLabel1; }
        set
        {
            if (value != emptyLabel1)
            {
                emptyLabel1 = value;
                NotifyPropertyChanged("EmptyLabel1");
            }
        }
    }
    public string EmptyLabel2
    {
        get { return emptyLabel2; }
        set
        {
            if (value != emptyLabel2)
            {
                emptyLabel2 = value;
                NotifyPropertyChanged("EmptyLabel2");
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是,我似乎无法对IsVisible进行任何有条件的检查.

Arv*_*iya 5

我没有试过这个,但设置Labelxaml的可见性需要像这样做

 <Label IsVisible="{Binding EmptyLabel1,
     Converter={StaticResource StringNullOrEmptyBoolConverter}"
     Text="{Binding EmptyLabel1}/>
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请检查

  • 另一种选择是向虚拟机添加一个属性: public bool ShowLabel1 { get { return !string.IsNullOrEmpty(emptyLabel1); } } 并在 Xaml 中绑定 IsVisible="{Binding ShowLabel1}"。 (2认同)