I have TreeView control and I need to bind property from root (window/usercontrol) DataContext in context menu in that treeview.
<TextBox Text="{Binding Header}"></TextBox> <TreeView ItemsSource="{Binding Items}" Grid.Row="1"> <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu> <MenuItem Header="{{ BINDING TO HEADER PROPERTY FROM WINDOW DATACONTEXT}}"/> </ContextMenu> </Setter.Value> </Setter> </Style> </TreeView.ItemContainerStyle> </TreeView> public ObservableCollection<string> Items { get; set; } public string Header { get { return _header; } set { _header = value; } } I've tried multiple things: I've added x:Name="WindowRoot" to Window and {Binding Header, ElementName=WindowRoot} but it didn't work, I've tried multiple FindAncestor and RelativeSource but it didn't work.
Can someone help me?
Edit:
This is simplified case, in my normal application I use Unity + Prism, so ViewModel is AutoDiscovered (prism:ViewModelLocator.AutoWireViewModel="True") and it generally works. By "works" I mean: TreeView shows items from my collection, so it is connected, the problem is with context menu binding only.
In this simplified example I have ugly and simple code-behind, because I only want to test this ContextMenu binding:
public partial class MainWindow : Window { public ObservableCollection<string> Items { get; set; } private string _header = "testtest"; public string Header { get { return _header ; } set { _header = value; } } public MainWindow() { Items = new ObservableCollection<string>(); Items.Add("ItemTest"); InitializeComponent(); this.DataContext = this; } }
window.DataContext = MyViewModel? Binding in the<TextBox Text="{Binding Header}"></TextBox>aboveTreeViewworks fine.