是否有可能在XAML画布的儿童属性绑定? [英] Is it possible to bind a Canvas's Children property in XAML?

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

问题描述

我有点惊讶,这是不可能通过XAML建立一个绑定Canvas.Children。我不得不求助于code隐藏方法,看起来是这样的:

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>

有没有办法做到这一点,而不诉诸于code隐藏的方法?我已经做了一些关于这个问题google搜索,但没有拿出太多了这一特定问题。

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.

我不喜欢我目前的做法,因为这不过这样会弄脏我漂亮的模型 - 视图 - 视图模型通过查看了解它的视图模型的。

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.

推荐答案

我不相信它可能使用与儿童属性绑定。其实,我试过,今天做它误了我喜欢做你。

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.

画布是一个非常简陋的容器。这真的是不适合这样的工作。你应该考虑的众多ItemsControls之一。您可以将数据模型的视图模型的的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画布的儿童属性绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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