简体   繁体   English

将集合绑定到网格

[英]Bind collection to Grid

I have an object called Fly , which has properties Position (Point) and Orientation (double). 我有一个名为Fly的对象,它具有Position (点)和Orientation (双)属性。 In my MainViewModel I have a collection of my custom object Fly called Flies . 在MainViewModel中,有一个名为Flies自定义对象Fly的集合。

The View of a fly is a .png image. 苍蝇的景色是.png图像。 I want to bind Flies to my Grid in MainWindow using their properties Position and Orientation to display the flies on the screen. 我想使用它们的属性“ Position和“ OrientationFlies绑定到MainWindow中的Grid ,以在屏幕上显示苍蝇。

I've never done this kind of binding of a collection before. 我以前从未做过这样的集合绑定。 What I did before is bind a collection to a ListBox or ItemsControl : 我之前所做的是将集合绑定到ListBoxItemsControl

        <ItemsControl ItemsSource="{Binding MyCollection}">
        <ItemsControl.Template>
            <ControlTemplate>
                <ScrollViewer>
                    <ItemsPresenter/>
                </ScrollViewer>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <local:ItemView/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

How could I bind my objects to a Grid or any other control to be able displaying the right position and angular orientation? 如何将对象绑定到Grid或任何其他控件以能够显示正确的位置和角度方向?

based on some assumptions from the question here is what you are looking for 根据问题的一些假设,这里是您要寻找的东西

<ItemsControl ItemsSource="{Binding MyCollection}">
    <ItemsControl.Resources>
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Left"
                    Value="{Binding Position.X}" />
            <Setter Property="Canvas.Top"
                    Value="{Binding Position.Y}" />
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <RotateTransform Angle="{Binding Orientation}" />
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.Resources>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:ItemView />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

What I have done 我做了什么

  • set the Canvas as the ItemsPanel of the ItemsControl which will host the items (flies) 将Canvas设置为ItemsControl的ItemsPanel,它将托管项目(苍蝇)
  • created a style targeting ContentPresenter which is the default host for the items in ContentPresenter. 创建了一个针对ContentPresenter的样式,该样式是ContentPresenter中项目的默认宿主。
  • binded the Canvas.Left & Canvas.Top to respective Position's X & Y 将Canvas.Left和Canvas.Top绑定到相应位置的X和Y
  • added RotateTransform for the orientation and binded the angle to Orientation property. 为方向添加了RotateTransform,并将角度绑定到Orientation属性。

I can assume that the flies are supposed to fly, so in that case you may want that changing X & Y would change the fly position. 我可以假设苍蝇应该飞行,​​因此在这种情况下,您可能希望更改X和Y会更改飞行位置。 but since Point class do not notify the changes to the sub properties, binding may not work as expected. 但是由于Point类不会通知对子属性的更改,因此绑定可能无法按预期进行。 So as a suggestion you may want to create your own Point class with property changed notifications. 因此,建议您使用属性更改通知创建自己的Point类。

To start with, you shouldn't set the ItemsControl.Template property unless you actually want to define a new ControlTemplate for it... there is certainly no point in replacing the default ControlTemplate with an identical one. 首先,除非您真的想为其定义一个新的ControlTemplate ,否则不应该设置ItemsControl.Template属性...用相同的默认ControlTemplate替换当然没有意义。 Next, your ItemView seems pointless if it is just an Image ... just use an Image instead. 接下来,如果您的ItemView只是一个Image ,则显得毫无意义……只需使用Image In this way, you'll be able to data bind your properties properly. 这样,您就可以正确地数据绑定属性。

There are several ways to acheive your requirements, but you could use a RotateTransform for the Orientation and could use a TranslateTransform to move the items. 有几种方法可以满足您的要求,但是您可以使用RotateTransform进行Orientation ,也可以使用TranslateTransform来移动项目。 Try something like this: 尝试这样的事情:

<ItemsControl ItemsSource="{Binding MyCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Image Source="/Images/Fly.png">
                <Image.RenderTransform>
                    <TransformGroup>
                        <RotateTransform Angle="{Binding Orientation}" />
                        <TranslateTransform X="{Binding Position.X}"
                            Y="{Binding Position.Y}" />
                    </TransformGroup>
                </Image.RenderTransform>
            </Image>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

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

相关问题 如何将集合绑定到网格WPF - how to bind collection to a grid WPF Telerik UI网格:绑定到模型的集合成员 - Telerik UI Grid: Bind to collection member of model 将可观察的集合模型属性绑定到网格中的下拉列 - Bind observable collection model property to dropdown column in grid 无法将数据网格绑定到我可观察的类对象集合 - Unable to bind Data Grid to my observable collection of class object 如何使用MVC Razor在编辑器模板中将Kendo UI Grid绑定到我的模型集合 - How to bind a Kendo UI Grid to a collection of my model in an editor template using MVC Razor 将图像绑定到集合中的网格视图的有效方法-Windows 8 Metro - Efficient way to bind images to a grid-view from a collection - Windows 8 Metro 如何在绑定到树的网格中绑定表格,而树则绑定到WPF中的其他集合 - How to bind a form in a grid that is bound to a tree which is bound to a different collection in wpf 如何将列表/字符串集合绑定到WPF中数据网格中列的行? - How can I bind a list/collection of strings to the rows of a column in a data grid in WPF? 将DropDownList绑定到Objects集合中的集合 - Bind DropDownList to a collection in a collection of Objects 将datagrid绑定到集合 - bind datagrid to collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM