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

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

问题描述

我目前正在尝试绑定到 wpf 集合中的某些项目.最好通过一个例子来解释这一点.

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

我的 XAML 如下:

My XAML is below:

<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.

我在 window_load 事件中有 C# 代码来将我的数据源绑定到我的椭圆,如下所示:

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;

现在我从我的收藏中绑定了帆布容器.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'.

当我运行代码集时,我的椭圆正确绑定到数据源,这很好,但由于椭圆没有设置为从集合中获取确切的行,因此默认情况下会获取最后一行,因此将我的椭圆设置为 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}"/>

如果要绑定集合中的所有项目,则需要使用带有 ItemTemplateItemsPanelItemsControl :

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天全站免登陆