WPF绑定到集合中的特定项目 [英] WPF Binding to specific items in collection

查看:166
本文介绍了WPF绑定到集合中的特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图绑定到在WPF集合中的某些项目。这最好通过一个例子说明。

I'm currently trying to bind to certain items in a collection in wpf. This is best explained by an example.

我的XAML是如下:

<Canvas Name="TaskCanvas" Width="467.667" Height="414">
  <Ellipse Name="myElipse" Fill="White" Stroke="Black" Width="126" Height="76"
           Canvas.Left="{Binding Path=XPos}" Canvas.Top="{Binding Path=YPos}" />
</Canvas>       

现在,你可以看到我只是绑定到属性,椭圆的一个简单的例子,将其定位在从我的数据源中的X和Y轴。

Now as you can see i am just binding to the properties as a simple example of the ellipse to position it on the x and y axis from my data source.

我有C#code在window_load事件对我的数据源绑定到我的椭圆形如下图所示:

I have c# code in the window_load event to bind my datasource to my ellipse as shown below:

PosClass posclass = new PosClass();
List<PosClass> posClasses = new List<PosClass>();

posclass.YPos = 100;
posclass.XPos= 100;            
posClasses.Add(posclass);

posclass.YPos = 0;
posclass.XPos = 0;
posClasses.Add(posclass);

TaskCanvas.DataContext = posClasses;

现在我做了绑定,从我的收藏画布cotainer。该PosClass是一个简单的类具有两个属性是XPOS'和'YPos。

Now I did a binding to the canvas cotainer from my collection. The PosClass is a simple class with two properties being 'XPos' and 'YPos'.

当我运行code设置我的椭圆正确绑定这是伟大的,但因为椭圆没有设置采取从集合默认情况下它一个确切的行占用的最后一排这样的设置我的椭圆形到数据源0,0的位置。

When i run the code set my ellipse is correctly bound to the datasource which is great but as the ellipse is not set to take an exact row from the collection it by default takes the last row so setting my ellipse to 0,0 position.

我要什么,能够做的是设置附加椭圆XAML中集合中使用的第一个项目,或者如果我有更多的项目可以说第10项。我再次想这样做在XAML所以目前我只是有约​​束力的X和Y位置,是否有某种语法,让我也指定要使用哪一行集合中?

What I want to be able to do is set the ellipse to use the first item in the collection attached in XAML or if i had more items lets say the 10th item. Again i want to do this in XAML so currently i just have the binding to the X and Y positions, is there some sort of syntax that lets me also specify which row in the collection to use?

推荐答案

您可以指定要绑定使用括号哪个项目:

You can specify which item you want to bind to using brackets :

<Ellipse Name="myElipse" Fill="White" Stroke="Black" Width="126" Height="76" Canvas.Left="{Binding Path=[10].XPos}" Canvas.Top="{Binding Path=[10].YPos}"/>

如果您要绑定在集合中的所有项目,你需要使用的ItemsControl 的ItemTemplate ItemsPanel

If you want to bind all items in the collections, you need to use an ItemsControl with an ItemTemplate and ItemsPanel :

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <Ellipse Name="myElipse" Fill="White" Stroke="Black" Width="126" Height="76" Canvas.Left="{Binding Path=XPos}" Canvas.Top="{Binding Path=YPos}"/>
    </ItemsControl.ItemTemplate>
</ItemsContol>

这篇关于WPF绑定到集合中的特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆