简体   繁体   English

wpf xaml 绑定到 Resource 中的 translateTransform

[英]wpf xaml binding to translateTransform in Resource

I have a path defined as a resource in a resourceDictionary.我在 resourceDictionary 中有一个定义为资源的路径。 In the code a rectangle with a transformgroup is defined.在代码中定义了一个带有变换组的矩形。 There are a few transforms to position the rectangle at a local datum then a translateTransform with a binding.有几个转换为 position 本地数据处的矩形,然后是带有绑定的 translateTransform。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <GeometryGroup x:Key="box">
        <RectangleGeometry Rect="0,0,70,25"/>
        <GeometryGroup.Transform>
            <TransformGroup>
                <ScaleTransform ScaleX="1" ScaleY="1"/>
                <TranslateTransform X="-37.5" Y="-12.5"/>
                <TranslateTransform X="500" Y="0"/>
                <RotateTransform Angle="0"/>
                <TranslateTransform X="{Binding BX}" Y="{Binding BY}"/>
            </TransformGroup>
            
        </GeometryGroup.Transform>
    </GeometryGroup>
</ResourceDictionary>

The viewmodel holds an observable collection of "box" objects.视图模型包含一个可观察的“盒子”对象集合。 Each box object has a BX and BY property to define the position of the box.每个盒子object都有一个BX和BY属性来定义盒子的position。 The xaml page displays those boxes. xaml 页面显示了这些框。

There is a data template as shown here which points to the resource:这里有一个指向资源的数据模板:

<DataTemplate DataType="{x:Type cfg:Box}">
    <Path Fill="White" Stroke="Black" Data="{StaticResource box}"/>
</DataTemplate>

Currently the boxes are displayed on a canvas with the following code:目前,这些框显示在 canvas 上,代码如下:

<ItemsControl ItemsSource="{Binding Boxes}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Left" Value="{Binding Path=BX}"/>
            <Setter Property="Canvas.Top"  Value="{Binding Path=BY}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

This does locate the shapes on the canvas by setting the Canvas.Left and Canvas.Top properties.这确实通过设置 Canvas.Left 和 Canvas.Top 属性定位了 canvas 上的形状。 But instead what I would like to do is bind the BX and BY values from each "box" object in the observablecollection to the translateTransform associated with each item that is bound to the itemssource.但是我想做的是将 observablecollection 中每个“框”object 的 BX 和 BY 值绑定到与绑定到 itemssource 的每个项目关联的 translateTransform。 (Right now the binding in the resource's translatetransform fails.) (现在资源的 translatetransform 中的绑定失败。)

So if I have 5 boxes in my observablecollection in the view model, and each one has a unique position in a BX and BY property (with an option to rotate, scale, etc) - how do I bind those properties to the corresponding items in the ItemsControl?因此,如果我在视图 model 中的 observablecollection 中有 5 个框,并且每个框在 BX 和 BY 属性中都有一个唯一的 position(可以选择旋转、缩放等)——我如何将这些属性绑定到相应的项目项目控制?

I don't follow why binding the contentpresenter canvas.left and top is not suitable.我不明白为什么绑定 contentpresenter canvas.left 和 top 是不合适的。

Any bindings on properties in your geometrygroup cannot work as you have it in a resource dictionary.几何组中属性的任何绑定都无法像在资源字典中那样工作。

Geometrygroup is a freezable and will be frozen if you put it in a resource dictionary. Geometrygroup 是可冻结的,如果您将其放入资源字典中,它将被冻结。 There is a built in mechanism calls Freeze() on any and all freezables you use in a resource dictionary.在资源字典中使用的任何和所有可冻结对象上都有一个内置机制调用 Freeze()。 That means those variables aren't going to change.这意味着这些变量不会改变。

https://learn.microsoft.com/en-us/do.net/api/system.windows.media.geometrygroup?view=windowsdesktop-7.0 https://learn.microsoft.com/en-us/do.net/api/system.windows.media.geometrygroup?view=windowsdesktop-7.0

Inheritance: Object DispatcherObject DependencyObject Freezable Animatable Geometry GeometryGroup Inheritance:Object DispatcherObject DependencyObject Freezable Animable Geometry GeometryGroup

You could put your geometrygroup in windows resources instead and it wouldn't be frozen.您可以将几何组放在 windows 资源中,它不会被冻结。

The translatetransform has an Xproperty and Yproperty which I've animated in the past so I think the binding could work if there's a suitable BX and BY property in the datacontext of the resource. translatetransform 有一个 Xproperty 和 Yproperty,我过去曾对其进行过动画处理,因此我认为如果资源的数据上下文中有合适的 BX 和 BY 属性,则绑定可以工作。

You should be able to do something like:您应该能够执行以下操作:

    <Style TargetType="ContentPresenter">
        <Setter Property="RenderTransform">
            <Setter.Value>
                <TransformGroup>
                    <RotateTransform x:Name="RotateTransform"
                             Angle="{Binding Angle}"
                             CenterX="{Binding CenterX}"
                             CenterY="{Binding CenterY}"/>
                    <ScaleTransform x:Name="MirrorTransform"
                                    ScaleX="{Binding MirroringX}"
                                    ScaleY="{Binding MirroringY}"
                                    CenterX="{Binding CenterX}"
                                    CenterY="{Binding CenterY}"/>
                    <ScaleTransform x:Name="ScaleTransform"
                                    ScaleX="{Binding ScaleX}"
                                    ScaleY="{Binding ScaleY}"/>
                </TransformGroup>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.ItemContainerStyle>

So long as you don't get your transform frozen by putting it in a resource dictionary.只要您不通过将其放入资源字典来冻结您的转换。

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

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