If you try to use TwoWay binding directly on for example a textbox in a listbox. You will have a problem.
<ListBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding ListOfStrings}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The result you want is obvious, you want to allow the TextBox manipulate the ListOfStrings ( oftype List<string> ).
The error you will get will look something like: Two-way binding requires Path or XPath.
The solution is this, it’s not pretty but it works !
<ListBox Grid.Column="1" Grid.Row="1" ItemsSource="{Binding ListOfStrings}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
good solution!!!
Comment by yossharel — January 20, 2010 @ 10:17 |
Thank you! I spent so much time trying to find a workaround!
Comment by Viki — October 7, 2010 @ 09:44 |
worked as well for me
Comment by Kirill Medvedev — December 13, 2010 @ 04:20 |
Sorry, I’ve posted a code, but it was removed in my previous message. TextBox Text=”{Binding .}” worked as well for me.
Comment by Kirill Medvedev — December 13, 2010 @ 04:26 |
Thanks. This solved my issue too…it was starting to get to me.
Comment by Jamie — April 27, 2011 @ 03:31 |
Hi,
I am having same problem with DataGrid and none of the solution is working in my case, I have tried all, with =”{Binding .}” and =”{Binding Path=.}” I am getting the same exception “Two-way binding requires Path or XPath.” and with Binding=”{Binding Path=DataContext, RelativeSource={RelativeSource Self}}” and Binding=”{Binding}}”, data that I type in row just vanishes as soon as focus is lost from that row. Any ideas?
Thanks,
Dami.
Comment by dami — November 9, 2011 @ 15:49 |
Code was there in originalpost but didn’t went, here is that again.
”
“
Comment by dami — November 9, 2011 @ 15:53 |
Posting code again
.
Comment by dami — November 9, 2011 @ 15:58
I have to thank you for the efforts you’ve put in writing this site. I’m hoping to view the same high-grade content from you in the future as well. In truth, your creative writing abilities has motivated me to get my own, personal site now
Comment by Tomiko Hamontree — March 7, 2012 @ 10:23 |