是否可以在 XAML 中绑定 Canvas 的 Children 属性? [英] Is it possible to bind a Canvas's Children property in XAML?

查看:39
本文介绍了是否可以在 XAML 中绑定 Canvas 的 Children 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点惊讶无法通过 XAML 为 Canvas.Children 设置绑定.我不得不求助于类似这样的代码隐藏方法:

I'm a little surprised that it is not possible to set up a binding for Canvas.Children through XAML. I've had to resort to a code-behind approach that looks something like this:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    DesignerViewModel dvm = this.DataContext as DesignerViewModel;
    dvm.Document.Items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Items_CollectionChanged);

    foreach (UIElement element in dvm.Document.Items)
        designerCanvas.Children.Add(element);
}

private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    ObservableCollection<UIElement> collection = sender as ObservableCollection<UIElement>;

    foreach (UIElement element in collection)
        if (!designerCanvas.Children.Contains(element))
            designerCanvas.Children.Add(element);

    List<UIElement> removeList = new List<UIElement>();
    foreach (UIElement element in designerCanvas.Children)
        if (!collection.Contains(element))
            removeList.Add(element);

    foreach (UIElement element in removeList)
        designerCanvas.Children.Remove(element);
}

我更愿意像这样在 XAML 中设置绑定:

I'd much rather just set up a binding in XAML like this:

<Canvas x:Name="designerCanvas"
        Children="{Binding Document.Items}"
        Width="{Binding Document.Width}"
        Height="{Binding Document.Height}">
</Canvas>

有没有办法在不诉诸代码隐藏方法的情况下实现这一目标?我已经对这个主题进行了一些谷歌搜索,但没有针对这个特定问题提出太多建议.

Is there a way to accomplish this without resorting to a code-behind approach? I've done some googling on the subject, but haven't come up with much for this specific problem.

我不喜欢我目前的方法,因为它让 View 知道它是 ViewModel,从而破坏了我的 Model-View-ViewModel.

I don't like my current approach because it mucks up my nice Model-View-ViewModel by making the View aware of it's ViewModel.

推荐答案

我不认为可以将绑定与 Children 属性一起使用.我今天确实尝试过这样做,但它和你一样出错了.

I don't believe its possible to use binding with the Children property. I actually tried to do that today and it errored on me like it did you.

Canvas 是一个非常初级的容器.它真的不是为这种工作而设计的.您应该查看众多 ItemsControl 之一.您可以将数据模型的 ViewModel 的 ObservableCollection 绑定到它们的 ItemsSource 属性,并使用 DataTemplates 来处理每个项目在控件中的呈现方式.

The Canvas is a very rudimentary container. It really isn't designed for this kind of work. You should look into one of the many ItemsControls. You can bind your ViewModel's ObservableCollection of data models to their ItemsSource property and use DataTemplates to handle how each of the items is rendered in the control.

如果您找不到以令人满意的方式呈现您的项目的 ItemsControl,您可能需要创建一个自定义控件来满足您的需求.

If you can't find an ItemsControl that renders your items in a satisfactory way, you might have to create a custom control that does what you need.

这篇关于是否可以在 XAML 中绑定 Canvas 的 Children 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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