本文介绍了是" {结合路径=}"和" {结合}"真正平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF项目中,我有一个显示从列表与LT项目列表框;串> 集合。我想让这些项目可编辑的文本,所以我包他们每个人在一个ItemTemplate一个文本框(可能不是最好的方式,但我是新来WPF)。我遇到了麻烦只是文本框Text属性绑定到每个项目的值。我用一个点或期限的路径属性终于偶然发现了一个例子( {绑定路径=}

In my WPF project, I have a ListBox that displays items from a List<string> collection. I wanted to make the text of these items editable, so I wrapped each of them in an ItemTemplate with a TextBox (might not be the best way, but I'm new to WPF). I was having trouble simply binding the TextBoxes' Text property to the value of each item. I finally stumbled upon an example using a single dot or period for its Path property ({Binding Path=.}):

<ListBox ItemsSource="{Binding ElementName=recipesListbox,Path=SelectedItem.Steps}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=.}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

不过,我不明白为什么简单地使用 {结合} 没有工作(它提出了双向绑定需要路径或XPath除外),如:

However I don't understand why simply using {Binding} didn't work (it raised a "Two-way binding requires Path or XPath" exception), as according to Microsoft:

[...]句点(。)的路径可以被用来结合到电流源。例如,文本={结合}是等同于文本={结合路径=}的

有人能阐明这一暧昧行为?

Could someone shed light on this ambiguous behavior?

编辑:此外,似乎 {绑定路径=} 不一定会让双向绑定,如修改的文字和动人焦点不会更新底层源(相同的源也显示性能和DataGrid控件修改成功)。我肯定在这里失去了一些东西。

Moreover, it seems {Binding Path=.} does not necessarily give two-way binding, as modifying the text and moving the focus does not update the underlying source (the same source has also properties displayed and successfully modified on a DataGrid control). I'm definitely missing something here.

推荐答案

的文档指出 {结合} 等同于 {绑定路径= } 。然而,它的的等同于 {绑定路径} 为您键入。如果包括路径属性,必须将其分配到的东西,无论是路径= 路径= OtherProperty

The documentation states that {Binding} is equivalent to {Binding Path=.}. However it is not equivalent to {Binding Path} as you have typed. If you include the Path property, you must assign it to something, be it Path=. or Path=OtherProperty.

这篇关于是&QUOT; {结合路径=}&QUOT;和&QUOT; {结合}&QUOT;真正平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:14