ItemsPanel是Canvas,如何设置孩子的Left和Top?

时间:2017-02-21 18:19:27

标签: c# wpf xaml canvas

我是WPF新手......

我正在尝试创建一个派生自ItemsControl的CustomControl。 我想我理解ItemsPresenter,ItemsPanel和ItemTemplate,但是我试图做的事情似乎并不起作用。

My ItemsPanel是一个Canvas,在我的ItemTemplate中,我想设置Canvas.Left和Canvas.Top属性。但是在那个范围内,Xaml似乎并不了解Canvas,我该怎么办?

这是Xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SlBoard">
<Style TargetType="{x:Type local:MyPanel}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyPanel}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ItemsPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate DataType="local:PanelItem">
                <!-- Here, Canvas is grayed out, xaml doesn't know that this is within a Canvas -->
                <Button Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}" Content="{Binding Name}"></Button>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

0 个答案:

没有答案