是“{Binding Path=.}”吗?和“{绑定}”真正平等

发布于 2024-10-29 00:07:35 字数 1083 浏览 1 评论 0原文

在我的 WPF 项目中,我有一个 ListBox,它显示 List 集合中的项目。我想让这些项目的文本可编辑,因此我将它们每个都包装在带有 TextBox 的 ItemTemplate 中(可能不是最好的方法,但我是 WPF 新手)。我在简单地将 TextBoxes 的 Text 属性绑定到每个项目的值时遇到了麻烦。我最终偶然发现了一个使用单个点或句点作为 Path 属性的示例 ({Binding Path=.}):

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

但是我不明白为什么只使用 {Binding} 没用。

它引发了“双向绑定需要 Path 或 XPath”异常,如 根据 Microsoft

[...] 句点 (.) 路径可用于绑定到当前源。例如,Text="{Binding}" 相当于 Text="{Binding Path=.}"

有人能阐明这种模棱两可的行为吗?

编辑:此外,似乎 {Binding Path=.} 不一定提供双向绑定,因为修改文本和移动焦点不会更新底层源(同一源还在 DataGrid 控件上显示并成功修改了属性)。我肯定在这里遗漏了一些东西。

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>

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:

[...] a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}"

Could someone shed light on this ambiguous behavior?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北凤男飞 2024-11-05 00:07:35

例外的要点大概是您无法双向绑定绑定源本身,因此它会尝试阻止您创建一个行为方式与您希望的方式不同的绑定。通过使用{Binding Path=.},您只是欺骗了错误检测。

(此外,文档错误或不准确的情况也并非闻所未闻,尽管我总体上非常喜欢 MSDN 文档,因为它通常包含人们感兴趣的关键点)

The point of the exception presumably is that you cannot two-way bind a binding-source itself, so it tries to prevent you from creating a binding which does not behave the way you would want it to. By using {Binding Path=.} you just trick the error detection.

(Also it's not unheard of that documentation is erroneous or inaccurate, though i do like the MSDN documentation a lot in general as it usually does contain the crucial points one is interested in)

百变从容 2024-11-05 00:07:35

文档指出 {Binding} 相当于 {Binding Path=.}。但是,它等于您键入的{Binding Path}。如果包含 Path 属性,则必须将其分配给某个内容,可以是 Path=.Path=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.

怂人 2024-11-05 00:07:35

简而言之,两者的区别类似于传统的按值传递和按引用传递的区别。 (FYR - 通过引用传递有什么区别与按值传递?

但是我不明白为什么简单地使用 {Binding} 不起作用(它引发了“双向绑定需要 Path 或 XPath”异常)

现在让我们假设 {Binding} 可以是用于双向绑定。一般来说,{Binding} 创建一个带有数据上下文的基于值的链接,该链接不允许更新数据上下文。

{Binding Path=.} 使用“Path”引用的内存区域创建基于引用的链接,允许通过引用更新值。(在本例中,“点”是当前数据上下文)。

希望这有帮助!

In short, the difference between the two is analogous with the difference between the traditional pass by value and pass by reference. (FYR - What's the difference between passing by reference vs. passing by value?)

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

Lets assume here for now that {Binding} can be used for two way binding. In general {Binding} creates a value based link with datacontext which does not allow updating the datacontext.

Whereas {Binding Path=.} creates reference based link with the memory area referenced by the 'Path' which allows updating the value through reference.(in this case 'dot' the current datacontext).

Hope this helps!

奢望 2024-11-05 00:07:35

这些不一样。如果您在 ConsoleMessages 是仅带有 {Binding} 的 ObservableCollection 字符串的情况下绑定它,您会得到“双向绑定需要 Path 或 XPath”。异常,其中 {Binding Path=.} 有效。这是 WPF 4.0...

    <ItemsControl x:Name="ConsoleOutput" ItemsSource="{Binding ConsoleMessages, Mode=OneWay}" MaxHeight="400">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=.}" BorderThickness="0" Margin="0" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

我的 2p 值...

These are not the same. If you bind this where ConsoleMessages is an ObservableCollection string with just {Binding} you get a "Two-way binding requires Path or XPath." exception where as {Binding Path=.} works. This is with WPF 4.0...

    <ItemsControl x:Name="ConsoleOutput" ItemsSource="{Binding ConsoleMessages, Mode=OneWay}" MaxHeight="400">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=.}" BorderThickness="0" Margin="0" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

My 2p worth...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文