1

I am new to WPF...

I am trying to create a CustomControl that derives from ItemsControl. I think I understand the ItemsPresenter, ItemsPanel and ItemTemplate, however what I'm trying to do doesn't seem to work.

My ItemsPanel is a Canvas, and in my ItemTemplate, I want to set the Canvas.Left, and Canvas.Top properties. But in that scope, Xaml doesn't seem to know about the Canvas, what can I do?

Here's the 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>
edeboursetty
  • 5,669
  • 2
  • 40
  • 67

0 Answers0