I am trying to write a user control that has an ItemsControl element whose ItemTemplate contains a text box that will allow TwoWay to bind. However, I have to make a mistake somewhere in my code, because the binding only works as if Mode = OneWay. This is a pretty simplified excerpt from my project, but it still contains a problem:
<UserControl x:Class="ItemsControlTest.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Grid> <StackPanel> <ItemsControl ItemsSource="{Binding Path=.}" x:Name="myItemsControl"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBox Text="{Binding Mode=TwoWay, UpdateSourceTrigger=LostFocus, Path=.}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> <Button Click="Button_Click" Content="Click Here To Change Focus From ItemsControl" /> </StackPanel> </Grid> </UserControl>
Here is the code for the above control:
using System; using System.Windows; using System.Windows.Controls; using System.Collections.ObjectModel; namespace ItemsControlTest {
And finally, here is the xaml for the main window:
<Window x:Class="ItemsControlTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:ItemsControlTest" Title="Window1" Height="300" Width="300"> <Grid> <src:UserControl1 /> </Grid> </Window>
Well, that's all. I am not sure why editing the TextBox.Text properties in the window does not seem to update the source property for the code binding, namely MyCollection. Pressing the button pretty much makes the problem look me in the face;) Please help me understand what I'm wrong about.
Thanx!
Andrew
wpf binding itemscontrol
Andrew
source share