I need to access the container\'s DataContext from a UserControl (a grid containing textboxes and a listbox: I need to insert items in this list box) that I created in WPF:
Add this BindingProxy class to your project:
using System.Windows;
namespace YourNameSpace
{
///
/// Add Proxy to Resources
/// Bind like
///
public class BindingProxy : Freezable
{
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy));
}
}
Data="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext}"
If you needed something more complex you could use a custom converter.Now you have access to that parent's DataContext: {Binding Data.MyCommand, Source={StaticResource BindingProxy}}