Is there an issue with databinding in WPF when you bind to the current source (Path=".") and using a converter? The two way binding doesn't seem to work in this situation.
I know I could change the path, but I want to be able to pass the "Name" value to the converter.
I can't get the following example to work:
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:WpfTest"
Title="Window1" Height="600" Width="600">
<Window.Resources>
<l:Person x:Key="myDataSource" Name="Cam"/>
<l:TestConverter x:Key="converter" />
</Window.Resources>
<StackPanel>
<StackPanel.DataContext>
<Binding Source="{StaticResource myDataSource}"/>
</StackPanel.DataContext>
<TextBox>
<TextBox.Text>
<Binding
Path="."
UpdateSourceTrigger="PropertyChanged"
Converter="{StaticResource converter}"
ConverterParameter="Name"
/>
</TextBox.Text>
</TextBox>
<TextBlock Text="{Binding Path=Name}"/>
</StackPanel>
</Window>
class TestConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Reverse(value, parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Reverse(value, parameter);
}
private static object Reverse(object value, object parameter)
{
if (value == null)
throw new ArgumentNullException("value", "value is null.");
if (parameter == null)
throw new ArgumentNullException("parameter", "parameter is null.");
Person p = (Person)value;
if (parameter.ToString() == "Name")
{
StringBuilder sb = new StringBuilder();
for (int i = p.Name.Length - 1; i >= 0; i--)
{
sb.Append(p.Name[i]);
}
return sb.ToString();
}
throw new NotImplementedException();
}
}
public class Person:INotifyPropertyChanged
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}