WPF Bind to itself

WpfData BindingBindingSelf

Wpf Problem Overview


I've got a WPF Window, and somewhere there is a ListView where I bind a List<string> to.

Now somewhere in my ListView there is a TextBox and the Content property is set to {Binding}.

But this is the shorthand. How do I write the full binding to bind to itself?

{Binding Path=Self} doesn't work, neither does {Binding Self} (where the latter is a shortcut for the former).

Wpf Solutions


Solution 1 - Wpf

Short answer:{Binding} is not a shortcut for "binding to itself" (in the sense of RelativeSource.Self). Rather, {Binding} is equivalent to {Binding Path=.}, which binds to the current source.


To elaborate: A binding has a source and a path. You can do a "binding to itself", for example, by using

<myUIControl myProperty="{Binding RelativeSource={RelativeSource Self}, Path=x}" />

This, however, sets the source to the control itself, so it will try to access property x of the UI control (rather than property x of the current data context). From how I understood your question, this is not what you want; in particular, it is not what {Binding} does: {Binding} keeps the source as it is (usually the DataContext of some parent element) and binds to the source itself (equivalent to Path=.).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSnakeView Question on Stackoverflow
Solution 1 - WpfHeinziView Answer on Stackoverflow