[英]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
和“ Orientation
将Flies
绑定到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
: 我之前所做的是将集合绑定到
ListBox
或ItemsControl
:
<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 我做了什么
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.