简体   繁体   中英

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?

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>

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM