WPF多个列表视图之间的绑定

时间:2022-10-05 14:01:55

I have two listviews, one for Images and one for Tags, which have a many-many relationship.

我有两个listview,一个用于图像,一个用于标签,它们之间有很多关系。

Both listviews are bound to ObservableCollection of EF objects. The Tag listview also has a checkbox column.

两个listview都绑定到对EF对象的观察汇总。标签列表视图也有一个复选框列。

When I select an Image from the listview I'd like the associated Tags to be checked on the other listview. I need TwoWay binding to create and remove the relationships based on being checked or not.

当我从listview中选择一个图像时,我希望在另一个listview中检查关联的标记。我需要TwoWay绑定来创建和删除基于检查与否的关系。

How can I do this?

我该怎么做呢?

2 个解决方案

#1


0  

in your VM you have two ObservableCollection of all Images and Tags... I think you can use two others ObservableCollection of associated Images or Tags, when you select an Image you can bind the associated Tags with this ObservableCollection and vise versa.

在您的VM中,您有两个对所有图像和标记的观察汇总……我认为您可以使用另外两个观察到关联的图像或标记的观察,当您选择一个图像时,您可以将相关的标记绑定到这个观察点,反之亦然。

Other solution you can bind the associated items with selected item ("SelectedItems={Binding Path=SelectedItem.Tags, ElementName=MyImageListBox}", or something like that).

其他解决方案可以将相关项与所选项绑定(“SelectedItems={Binding Path=SelectedItem”)。标签,ElementName=MyImageListBox},或者类似的东西)。

#2


0  

I've got this working as follows:

我的工作原理如下:

  • I have extended my Tag model to have an MatchesImage boolean property
  • 我已经扩展了我的标记模型,使其具有MatchesImage boolean属性
  • I have bound the checkbox in my listviewitem twoway to this property
  • 我已经将listviewitem中的复选框绑定到此属性
  • The getter gets the CurrentImage and returns CurrentImage.Tags.Contains(this)
  • getter获取CurrentImage并返回CurrentImage. tag . contains (this)
  • When I change the checkbox in the property setter I retrieve the CurrentImage from my viewmodel (my viewmodels are global resources) and add/remove the Tag from it's Tags collection based on value
  • 当我在属性设置器中更改复选框时,我从我的viewmodel(我的viewmodel是全局资源)检索CurrentImage,并根据值从它的标记集合中添加/删除标记
  • I will probably change CurrentImage to be accessible via a data repository, instead of the viewmodel
  • 我可能会将CurrentImage更改为可以通过数据存储库访问,而不是通过viewmodel访问

Something along the lines of:

类似于:

public partial class Tag : INotifyPropertyChanged

  public bool MatchesImage {
    get
    {
        Image img = DataRepository.CurrentImage;
        return (img != null) ? this.Images.Contains(img) : false;
    }
    set
    {
        Image img = DataRepository.CurrentImage;
        if (img != null)
        {
            if (value)
                img.Tags.Add(this);
            else
                img.Tags.Remove(this);
            OnPropertyChanged("MatchesImage");
        }
    }
  }
}

#1


0  

in your VM you have two ObservableCollection of all Images and Tags... I think you can use two others ObservableCollection of associated Images or Tags, when you select an Image you can bind the associated Tags with this ObservableCollection and vise versa.

在您的VM中,您有两个对所有图像和标记的观察汇总……我认为您可以使用另外两个观察到关联的图像或标记的观察,当您选择一个图像时,您可以将相关的标记绑定到这个观察点,反之亦然。

Other solution you can bind the associated items with selected item ("SelectedItems={Binding Path=SelectedItem.Tags, ElementName=MyImageListBox}", or something like that).

其他解决方案可以将相关项与所选项绑定(“SelectedItems={Binding Path=SelectedItem”)。标签,ElementName=MyImageListBox},或者类似的东西)。

#2


0  

I've got this working as follows:

我的工作原理如下:

  • I have extended my Tag model to have an MatchesImage boolean property
  • 我已经扩展了我的标记模型,使其具有MatchesImage boolean属性
  • I have bound the checkbox in my listviewitem twoway to this property
  • 我已经将listviewitem中的复选框绑定到此属性
  • The getter gets the CurrentImage and returns CurrentImage.Tags.Contains(this)
  • getter获取CurrentImage并返回CurrentImage. tag . contains (this)
  • When I change the checkbox in the property setter I retrieve the CurrentImage from my viewmodel (my viewmodels are global resources) and add/remove the Tag from it's Tags collection based on value
  • 当我在属性设置器中更改复选框时,我从我的viewmodel(我的viewmodel是全局资源)检索CurrentImage,并根据值从它的标记集合中添加/删除标记
  • I will probably change CurrentImage to be accessible via a data repository, instead of the viewmodel
  • 我可能会将CurrentImage更改为可以通过数据存储库访问,而不是通过viewmodel访问

Something along the lines of:

类似于:

public partial class Tag : INotifyPropertyChanged

  public bool MatchesImage {
    get
    {
        Image img = DataRepository.CurrentImage;
        return (img != null) ? this.Images.Contains(img) : false;
    }
    set
    {
        Image img = DataRepository.CurrentImage;
        if (img != null)
        {
            if (value)
                img.Tags.Add(this);
            else
                img.Tags.Remove(this);
            OnPropertyChanged("MatchesImage");
        }
    }
  }
}