如何更改 ObservableCollection 中每个项目的数据? [英] How to change data in every item in ObservableCollection?

查看:339
本文介绍了如何更改 ObservableCollection 中每个项目的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XAML 中有一个这种结构,其中每个项目都有复选框.

I have a this structure in XAML where every item has checkbox.

□ Fruits
   □ Apple
   □ Banana
   □ Orange

我的目标是有一个功能,如果水果复选框被选中,它将检查所有(选择所有)水果.

My goal is to have a function that if Fruit's checkbox is checked, it will check all (select all) fruits.

所以我在我的 Checkbox 的 IsChecked 中绑定了我的 ViewModel 的函数.

So I am binding my ViewModel's function in my Checkbox's IsChecked.

<TreeView
    ItemsSource="{Binding Foods}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type domain:Food}" ItemsSource="{Binding Fruits}">
            <StackPanel Orientation="Horizontal">
                <CheckBox
                    IsChecked="{Binding SelectAll}"
                    />
                <TextBlock Text="   "/>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type domain:Fruit}">
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

我的ViewModel是:

private bool _selectAll = false;

public bool SelectAll
{
    get
    {
        return _selectAll;
    }
    set
    {
        foreach (var item in Fruits)
        {
            item.isSelected = true;
        }
    }
}

编辑

public class Food
{
    public string Name { get; set; }
    public ObservableCollection<Fruit> Fruits { get; set; } = new ObservableCollection<Fruit>();
}

public class Fruit
{
    public string Name { get; set; }
    public bool IsSelected { get; set; }
}

但是什么都没发生.

推荐答案

我不完全确定您的期望,但是对于问题中给出的 XAML,我什至没有在单个 Fruit 前面的复选框名称,所以我不得不稍微编辑 XAML:

I'm not entirely sure what you were expecting, but with the XAML given in the Question I didn't even have check boxes in front of the individual Fruit names, so I had to edit the XAML a little:

<TreeView
    ItemsSource="{Binding Foods}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type domain:Food}" ItemsSource="{Binding Fruits}">
            <StackPanel Orientation="Horizontal">
                <CheckBox
                    IsChecked="{Binding SelectAll}" />
                <TextBlock Text="   " />
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type domain:Fruit}">
            <!-- Here I added the StackPanel and the CheckBox -->
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsSelected}" />
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

之后我做了一些代码更改,主要是我只是实现了INotifyPropertyChanged.所以 Food 和 Fruit 类现在看起来像这样:

After that I did some code changes, mainly I just implemented INotifyPropertyChanged. So the Food and Fruit class look like this now:

public class Food
{
    public string Name { get; set; }

    public ObservableCollection<Fruit> Fruits { get; set; }

    private bool _selectAll;

    public bool SelectAll
    {
        get => _selectAll;
        set
        {
            _selectAll = value;
            foreach (var f in Fruits)
                f.IsSelected = value;
        }
    }
}

还有这样的水果

public class Fruit : INotifyPropertyChanged
{
    public string Name { get; set; }

    private bool _isSelected;

    public bool IsSelected
    {
        get => _isSelected;
        set
        {
            _isSelected = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

食品不需要 INotifyPropertyChanged

INotifyPropertyChanged isn't needed on Food

这篇关于如何更改 ObservableCollection 中每个项目的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆