简体   繁体   English

ItemsControl和控件的附加属性

[英]ItemsControl and controls attached properties

I'm trying to implement a diagram with movable/resizeable parts in WPF. 我正在尝试在WPF中实现带有可移动/可调整大小的部件的图表。 I would like to use ItemsControl with ItemsPanel configured to be "DynamicCanvas". 我想将ItemsControl与ItemsPanel配置为“ 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. 您现在只需要了解DynamicCanvas,就可以像普通画布一样工作-除了一个例外-它利用附加的属性在其子级上存储有关X,Y属性的信息。

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): DynamicCanvas上显示的控件是我的自定义类型(仅在最重要的部分下方):

<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). 此处不起作用的部分是具有附加属性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 1)DynamicCanvas期望其直接子级具有c:DynamicCanvas.Left和c:DynamicCanvas.Top定义

2) ItemsPanel when putting TableControls onto the DynamicCanvas wraps them in some kind of container 2)将TableControls放入DynamicCanvas时,ItemsPanel会将它们包装在某种容器中

3) DynamicCanvas sees no attached properties on its immediate children => treats them as being positioned at (0,0) and renders them effectively unmoveable. 3)DynamicCanvas在其直接子级上看不到任何附加属性=>将其视为位于(0,0)处,并使其实际上无法移动。

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 . 您必须在ItemContainerStyle修改项目包装器的ControlTemplate 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 ). 如果将其设置为简单的ContentPresenter ,则项目将不会被包裹在任何东西中( DataTemplate的内容将直接粘贴到DynamicCanvas )。

See this article . 看到这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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