The problem with the code below is: the binding to SomeClassProp.SubTextProp
doesn't work (the source property is not being set to textbox content), while to TextProp
it does.
XAML:
<Window x:Class="TestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Name="wMain"
SizeToContent="WidthAndHeight">
<StackPanel>
<TextBox Text="{Binding ElementName=wMain, Path=SomeClassProp.SubTextProp}" Width="120" Height="23" />
<TextBox Text="{Binding ElementName=wMain, Path=TextProp}" Width="120" Height="23" />
</StackPanel>
</Window>
and the code:
public partial class MainWindow : Window
{
public SomeClass SomeClassProp { get; set; }
public string TextProp { get; set; }
public MainWindow()
{
InitializeComponent();
SomeClassProp = new SomeClass();
}
}
public class SomeClass
{
public string SubTextProp { get; set; }
}
Am I missing something obvious here?
Note that I need this binding to work from target (textbox) to source (class property).
UPDATE: When I change the binding from ElementName=wMain
to RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}
- BOTH bindings work. So the problem is specific to ElementName
binding property.
SubTextProp
remains 'null', whileTextProp
is set to whatever I type into textbox. – GwenniDataContext
than with its own xaml, you would only need the Path part. If you created a class to holdTextProp
andSomeClassProp
and assigned an instance of that to the window'sDataContext
instead, you could do {Binding SomeClassProp.SubTextProp}. – Helsell