kubal5003
kubal5003

Reputation: 7254

ItemsControl and controls attached properties

I'm trying to implement a diagram with movable/resizeable parts in WPF. I would like to use ItemsControl with ItemsPanel configured to be "DynamicCanvas". All you need to know about DynamicCanvas right now is that it acts like a usual canvas - with one exception - it utilizes attached properties to store information about X,Y attributes on its children.

My code:

<ItemsControl IsTabStop="False" ItemsSource="{Binding ElementName=comboBox1,Path=SelectedItem.Source.Table}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <s:TableControl Table="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemContainerStyle>
        <Style>

        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <!--<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">-->
            <c:DynamicCanvas SizeHeightToContent="True" SizeWidthToContent="True"  ClipToBounds="True" SnapsToDevicePixels="True" PreviewMouseDown="Canvas_MouseDown" IsHitTestVisible="True" Background="Gray" >


            </c:DynamicCanvas>
            <!--</ScrollViewer>-->
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

The controls that are being displayed on DynamicCanvas are of my custom type (below only the most important part):

<ContentControl x:Class="SubiektCommerceSynchro.ViewModel.TableControl"
                c:DynamicCanvas.Left="{Binding X,Mode=TwoWay}" 
                c:DynamicCanvas.Top="{Binding Y,Mode=TwoWay}"
                Width="450"  Height="300"
></ContentControl>

Now the problem and the question:

The part here that doesn't work is with attached properties c:DynamicCanvas.Left(Top). Lets put it in steps:

1) DynamicCanvas expects its immediate children to have c:DynamicCanvas.Left and c:DynamicCanvas.Top defined

2) ItemsPanel when putting TableControls onto the DynamicCanvas wraps them in some kind of container

3) DynamicCanvas sees no attached properties on its immediate children => treats them as being positioned at (0,0) and renders them effectively unmoveable.

How can I resolve this issue?

Upvotes: 2

Views: 749

Answers (2)

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

Does this help?

        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="c:DynamicCanvas.Left"
                        Value="{Binding X,Mode=TwoWay}"/>
                <Setter Property="c:DynamicCanvas.Top" 
                        Value="{Binding Y,Mode=TwoWay}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>

Upvotes: 3

Matěj Z&#225;bsk&#253;
Matěj Z&#225;bsk&#253;

Reputation: 17272

You have to modify the ControlTemplate of the item wrapper in the ItemContainerStyle. If you set it to simple ContentPresenter, the items will not be wrapped in anything (the contents of the DataTemplate will be pasted directly into the DynamicCanvas).

See this article.

Upvotes: 1

Related Questions