【问题标题】:toggling visibility of a label in a listview在列表视图中切换标签的可见性
【发布时间】:2020-04-06 23:43:57
【问题描述】:

我是 Xamarin 的新手。我有一个列表视图,它绑定到一个 ObservableCollection,数据来自 sqlite。

列表视图有两个标签。当有人单击工具栏菜单按钮时,我想隐藏其中一个标签(lblGroup)。此代码无效。

代码如下:

<StackLayout>
    <ListView x:Name="lstItems" HasUnevenRows="True" ItemSelected="lstItems_ItemSelected" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout VerticalOptions="StartAndExpand"  Padding="20, 5, 20, 5" Spacing="3">
                        <Label x:Name="lblItemName" IsVisible="{Binding IsNameVisible}" Text="{Binding ItemName}" ></Label>
                        <Label x:Name="lblGroup" IsVisible="{Binding IsGroupVisible}" Text="{Binding ItemGroup}" ></Label>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

在 xaml.cs 文件中,我将 ObservableCollection 绑定到我的列表视图。

public ObservableCollection<Items> itemsObs;
public ItemDetails()
{
    InitializeComponent();
    LoadItems();
}

private async LoadItems()
{
    List<Items> items = _con.QueryAsync<Items>(Queries.ItemsById(ItemsId));
    itemsObs = new ObservableCollection<Items>(items);
    lstItems.ItemsSource = itemsObs ;
}

private void menu_Clicked(object sender, EventArgs e)
{
    itemsObs.ToList().ForEach(a => a.IsGroupVisible = false);
}

【问题讨论】:

  • Items 是否实现了 INotifyPropertyChanged?当您的 ItemsSource 为 itemsObs 时,您为什么要修改 versesObs
  • versesObs 粘贴错误。将其更新为 itemsObs
  • @GurmeetKhalsa 请看看我的回复,现在解决了你的问题吗?

标签: listview xamarin observablecollection


【解决方案1】:

作为 Jason 的回复,我猜您没有为 Items 类中的 IsGroupVisible 属性实现 INotifyPropertyChanged 接口,请像这样修改您的 Items 类:

 public class Items:ViewModelBase
{
    private bool _IsNameVisible;
    public bool IsNameVisible
    {
        get { return _IsNameVisible; }
        set
        {
            _IsNameVisible = value;
            RaisePropertyChanged("");

        }
    }

    private bool _IsGroupVisible;
    public bool IsGroupVisible
    {
        get
        { return _IsGroupVisible; }
        set
        {
            _IsGroupVisible = value;
            RaisePropertyChanged("IsGroupVisible");
        }
    }

    public string ItemName { get; set; }
    public string ItemGroup { get; set; }
}

ViewModelBase 类正在实现 INotifyPropertychanged,以通知数据更改。

public class ViewModelBase : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

而你设置了lstItems.ItemsSource = itemsObs,但是你改变了versesObs,什么是versesObs,我觉得你应该改变itemsObs。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2010-12-30
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多