99

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

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
Anemoia
  • 7,928
  • 7
  • 46
  • 71
  • I want to mention to a post related to this topic.The above answer is totally correct but there is a point that completes the above answer. Here is the [link](http://stackoverflow.com/questions/11995318/how-do-i-bind-to-relativesource-self). – Ali Fattahian Jun 23 '13 at 05:53

2 Answers2

278

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

John Cummings
  • 1,949
  • 3
  • 22
  • 38
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • I have a DataGrid where if the user accesses one of its ContextMenu's MenuItem's Command's via an InputBinding's KeyBinding whose `CommandParameter="{Binding ElementName=MyDataGrid, Path=SelectedItems}"`, it'll pass the SelectedItems to the Bound ICommand. However, `null` is passed if it's accessed via the ContextMenu. I've tried `CommandParameter=` "{Binding SelectedItems}"`, `"{Binding ElementName=MyDataGrid, Path=SelectedItems}"` and `"{Binding RelativeSource={RelativeSource Self}, Path=SelectedItems}"`. – Tom May 08 '17 at 21:03
  • @Tom: This is hard to answer in a comment. Please create a new question and provide a [mcve]. – Heinzi May 09 '17 at 07:26
0

another way to bind to self :

{Binding {}}

an empty {} binds to the RelativeSource.Self.

The Doctor
  • 636
  • 1
  • 7
  • 23