当使用ItemsControl ItemsControl.ItemsPanel设置为Canvas时,ContenPresenter进入并打破我对Canvas的子属性[WPF] [英] When using ItemsControl ItemsControl.ItemsPanel is set to Canvas, ContenPresenter comes in and break my Canvas properties on the children [WPF]

查看:1343
本文介绍了当使用ItemsControl ItemsControl.ItemsPanel设置为Canvas时,ContenPresenter进入并打破我对Canvas的子属性[WPF]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ItemsControl,其中ItemsPanel设置为Canvas(请参阅这个问题)。
ItemsControl按我想要的方式执行,当它通过将它放到ItemsControl.Items中来手动添加子元素时,它的作用就像一个charm:

I am using an ItemsControl where the ItemsPanel is set to Canvas (see this question for more background information). The ItemsControl is performing as I want, and it works like a charm when adding a child element manually by putting it into ItemsControl.Items:

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Items>
        <Button Canvas.Left="500" Content="Button Text" />
    </ItemsControl.Items>
</ItemsControl>

注意Button上的Canvas.Left属性。这个工作方式像一个魅力,按钮放在距离ItemsControl左侧500像素。很棒!

Note the Canvas.Left property on the Button. This works like a charm, and the Button is placed 500 pixels from the left of the ItemsControl left side. Great!

但是,当我定义一个ItemsSource绑定到一个List,Canvas.left没有任何效果:

However, When I am defining a ItemsSource binding to a List, the Canvas.left doesn't have any effect:

<ItemsControl ItemsSource="{Binding Elements}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Canvas.Left="500" Content="Button Text" />
        </DataTemplate>     
    </ItemsControl.ItemTemplate>
</ItemsControl> 

通过在运行时检查应用程序,我看到一个区别。在Canvas和按钮之间添加了容器ContentPresenter。

By inspecting the application during run time, I see one difference. The container ContentPresenter has been added between the Canvas and the button..

如何在ContentPresenter本身上设置Canvas.Left属性?或者有其他方法来解决这个问题吗?

How can I set the Canvas.Left property on the ContentPresenter itself? Or is there another way to solve this problem?

感谢所有!

推荐答案

我有几种解决方案:


  1. 使用layout / rendertransform而不是附加的属性

  2. 使用边距而不是附加的属性

  3. 从ItemsControl派生,并覆盖生成子容器的行为。 (GetContainerForItemOverride,IsItemItsOwnContainerOverride)。本文将非常好地解释其工作原理: http ://drwpf.com/blog/2008/07/20/itemscontrol-g-is-for-generator/

  1. use a layout/rendertransform instead of the attached property
  2. use margin instead of the attached property
  3. derive from ItemsControl, and override the behavior how the child containers are generated. (GetContainerForItemOverride, IsItemItsOwnContainerOverride). This article is explaining quite nicely how it works: http://drwpf.com/blog/2008/07/20/itemscontrol-g-is-for-generator/

这篇关于当使用ItemsControl ItemsControl.ItemsPanel设置为Canvas时,ContenPresenter进入并打破我对Canvas的子属性[WPF]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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