OK. It’s WPF and I’m trying to bind my window to my ViewModel. The VM looks like this:
public class VM
{
public SalesRecord SR {get; set;}
public List<string> AllSalesTypes {get; set;}
}
public class SalesRecord
{
public int ID {get; set;}
public DateTime Date {get; set;}
}
Here’s my XAML:
...
<TextBox Text="{Binding Path=ID, Mode=TwoWay}" />
<TextBox Text="{Binding Path=Date, Mode=TwoWay}" />
<ComboBox ItemsSource="{Binding AllSalesTypes}" Text="{Binding Path=SalesType, Mode=TwoWay}" />
...
I’m setting the data context to a VM object at runtime like this:
this.DataContext = _vm.SR;
Now the binding expressions work for all my TextBoxes that are point to the properties of SR object (e.g. ID and Date), but the ComboBox that needs to show the list of all SalesTypes does not work, obviously because AllSalesTypes is a member of the VM class.
My questions is: Is there a way to write a binding expression that looks into the parent of the current DataContext instead of itself?
I am not aware of any way you can use Binding to get the Parent of the DataContext.
Just like if you have a class
Awhich has a property namedMyPropertyand you write:You have no way to find
MyAInstancefrom your instance ofo.Your options are either using:
this.DataContext = _vmand accessing the properties like this:OR add a
Parentproperty toSalesRecordand manually set it to point to the VM, then something like this will work: