将列表视图项目计数绑定到 Xamarin 表单中的标签可见性

vam*_*usz 4 c# xamarin xamarin.forms

我在 Xamarin Forms 中绑定时遇到问题。我想设置IsVisible的属性Label,以true/false根据计数的项目Listview。如果Listview有任何项目,Label IsVisible将是false,否则将是true。是否可以在 Xamarin Forms 中进行绑定?我尝试这样做,但我不知道如何将数字转换0boolean falseXAML。

Dam*_*ian 10

您可以使用DataTrigger纯粹在 XAML 中执行此操作

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="ListViewTriggerToHideLabel.MainPage">
    <StackLayout>
        <Label Text="Welcome to Xamarin Forms!" IsVisible="False">
            <Label.Triggers>
                <DataTrigger TargetType="Label"
                             Binding="{Binding Source={x:Reference TheListView}, Path=ItemsSource.Count}" 
                             Value="0">
                    <Setter Property="IsVisible" Value="True" />
                </DataTrigger>
            </Label.Triggers>
        </Label>
        <ListView x:Name="TheListView" />
        <Button Text="Add an item" Clicked="Button_OnClicked" />
    </StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

处理按钮点击和初始化列表内容的代码隐藏(我通常使用数据绑定,但为了简单起见,我在示例中使用了代码隐藏):

using System;
using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace ListViewTriggerToHideLabel {
  public partial class MainPage : ContentPage {
    private readonly ObservableCollection<string> _items = new ObservableCollection<string>();

    public MainPage() {
      InitializeComponent();
      TheListView.ItemsSource = _items;
    }

    private void Button_OnClicked(object sender, EventArgs e) {
      _items.Add("Ouch");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Count属性的绑定有效,因为ItemsSourceObservableCollection


Tim*_*äki 4

这当然是可能的。我假设您已经有办法获取 ListView 中的项目计数,因此这里介绍了如何将该数字转换为 XAML 中的布尔值。

您需要的是IValueConverter接口的自定义实现。它可以获取绑定提供的值并将其转换为其他内容,并在需要时返回。

在您的情况下,您想要获取一个整数并返回一个布尔值。如果该值为零,则返回 true,否则返回 false。

public class CountToBoolenConverter : IValueConverter
{
    public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {            
        return (int)value != 0;
    }

    public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException ();
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来,您需要在 XAML 中定义命名空间。在文件顶部,您应该添加如下内容:

xmlns:local="clr-namespace:YourNameSpace;assembly=YourAssembly"
Run Code Online (Sandbox Code Playgroud)

将转换器添加到页面资源的开始标记之后<ContentPage>(或您拥有的任何类型的页面):

<ContentPage>
  <ContentPage.Resources>
    <ResourceDictionary>
      <CountToBoolenConverter x:Key="countToBoolean" />
    </ResourceDictionary>
  </ContentPage.Resources>

  <!-- Rest of the page -->
Run Code Online (Sandbox Code Playgroud)

最后使用转换器将 IsVisible 属性设置为您的标签:

<Label IsVisible="{Binding ItemCount, Converter={StaticResource countToBoolean}}">
Run Code Online (Sandbox Code Playgroud)

ItemCount 是视图模型中的一个整数属性,它应该包含 ListView 项目的计数。您可能已经有一种为 ListView 加载项目集合的方法,因此弄清楚如何完成此属性应该很简单。