WPF 如何使用值转换器绑定两个不同但相关类型的可观察集合?

发布于 2024-10-19 12:31:04 字数 146 浏览 1 评论 0原文

我有一个自定义控件,其中包含一个可观察集合(DP),其中包含某种类型的对象。我想将其绑定到另一个可观察集合,该集合在我的虚拟机中保存不同类型的对象。我该怎么做?

我应该做这样的事情吗?

编辑。当然,当集合中的元素在任一侧被修改时,绑定应该起作用。

I have a custom control with an observable collection(DP) holding a certain type objects. I want to bind it to another observable collection holding a different type objects in my VM. How do I do this?

Should I even be doing something like this?

Edit. Ofcourse the binding should work when elements in the collections are modified on either side.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗼ふ静 2024-10-26 12:31:04

Meleak 的评论是正确的,您应该以某种方式将一个集合转换为另一个集合并同步它们。这是我上次的做法:

View(我在下面将其命名为 MyControl)具有 IEnumerable 类型的属性 Items< br>
DataContext 具有 IEnumerable 类型的属性 Items

class CollectionsConverter : IValueConverter
{
    public object Convert(object value, ...)
    {
        var source = (ObservableCollection<Source>)value;
        var target = new ObservableCollection<Target>(source.Select(/* Convert items somehow /);
        // subscribe to both target's and source's 'CollectionChanged' events
        // and propagate them back and forth to another collection.
        // Propagated events should have converted items of course

        return target;
    }
    ...
}

然后在 XAML 中:

<MyControl Items="{Binding Items, Converter=CollectionsConverter}" />

关于转换每个项目 - 它可以是通用代码,它将动态确定如何转换 SourceTarget ,反之亦然,或者它应该是一个代码,它知道它将转换哪些确切类型以及如何转换它们。

Meleak's comment is correct, you should somehow transform one collection into another and sync them. Here is how I did it last time:

View (I've named it MyControl below) has property Items of type IEnumerable<Target>
DataContext has property Items of type IEnumerable<Source>

class CollectionsConverter : IValueConverter
{
    public object Convert(object value, ...)
    {
        var source = (ObservableCollection<Source>)value;
        var target = new ObservableCollection<Target>(source.Select(/* Convert items somehow /);
        // subscribe to both target's and source's 'CollectionChanged' events
        // and propagate them back and forth to another collection.
        // Propagated events should have converted items of course

        return target;
    }
    ...
}

Then in XAML:

<MyControl Items="{Binding Items, Converter=CollectionsConverter}" />

Regarding converting each item - it can be either generic code which will dynamically determine how to convert Source to Target and vice versa or it should be a code which will know which exact types it will convert and how to convert them.

落叶缤纷 2024-10-26 12:31:04

ValueConverters 不按照您描述的方式工作,您必须手动创建一个新集合。

这并不能直接回答你的问题,但是在ReactiveUI(http://www.reactiveui.net)中,这种情况非常简单:

var derivedColl = someCollection.CreateDerivedCollection(x => new SomeOtherClass(x));

ValueConverters don't work the way you're describing, you have to create a new collection by-hand.

This doesn't answer your question directly, but in ReactiveUI (http://www.reactiveui.net), this scenario is quite easy:

var derivedColl = someCollection.CreateDerivedCollection(x => new SomeOtherClass(x));
你在看孤独的风景 2024-10-26 12:31:04

使用实现 IValueConverter 的自定义 Converter 类



    public class ObserverTypeConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //return your dependency type from here     
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //return your view model type from here, this will get fired if your binding is two way
        }
    }


Use your custom Converter class implementing IValueConverter



    public class ObserverTypeConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //return your dependency type from here     
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //return your view model type from here, this will get fired if your binding is two way
        }
    }


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文