绑定到当前项目(WPF) [英] Binding to the Current Item (WPF)

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

问题描述

我试图绑定一个的ListView 控制在数据表,但WPF绑定系统似乎抱怨关于绑定路径我指定。

I am attempting to bind a ListView control to a DataTable, but the WPF binding system seems to be complaining about the binding path I specify.

作为一个例子,一个 GridViewColumn 定义如下:

As an example, a GridViewColumn is defined as follows:

<GridViewColumn Header="ColumnTitle" 
 DisplayMemberBinding="{Binding Path=/, 
                        Converter={StaticResource myConverter}}"/>

据我了解(和MSN似乎支持我),指定路径= / 应该对数据收集的当前项目的绑定。

As far as I understand (and MSN seems to support me), specifying Path=/ should make the binding on the current item of the data collection.

我收到(在跟踪窗口)的错误是:

The error I receive (in the trace window) is:

System.Windows.Data错误:39:BindingEx pression路径错误:''
  财产上的'OrdersRow'集合当前项目'未找到
  (哈希code = 680171)。 BindingEx pression:路径= /;的DataItem ='OrdersRow
  (哈希code = 680171);目标元素是'TextBlock的'(名称='');目标
  属性是文本(类型'字符串')​​

System.Windows.Data Error: 39 : BindingExpression path error: '' property not found on 'current item of collection' ''OrdersRow' (HashCode=680171)'. BindingExpression:Path=/; DataItem='OrdersRow' (HashCode=680171); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

这是给我的IM pression的 / 甚至不是一个有效的路径和WPF的期待斜线后的东西。如果是这样,怎么回事我会绑定到当前项目?为什么会出现这个错误摆在首位?

This is giving me the impression that / isn't even a valid path, and WPF is expecting something after the slash. If so, how else would I bind to the current item? Why am I getting this error in the first place?

推荐答案

我觉得困惑的是,在DataContext为GridViewColumn是的的顶部集合,但已经绑定到该项目该列,所以你并不需要指定路径。

I think the confusion is that the DataContext for the GridViewColumn is not the top collection, but is already the item that is bound to that column, so you don't need to specify a path.

这对你可能会使用这样的路径的时间是,如果你的控件的DataContext的是一种服务,您要绑定到选定的项目。一个可能的例子是。

The time that the you may use a path like this is if your control's DataContext is a List and you want to bind to the selected item. A possible example would be.

<Combobox DataContext={Binding ColourList}
          DataSource={Binding} <!--Bind to the datacontext -->
          ForeColor={Binding/} <!--Bind to the currently selected item 
                                   in the datacontext -->
          />

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

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