Okay - I am having a little trouble following what you are trying to accomplish - but try the following.
Up vote 0 down vote favorite share g+ share fb share tw.
Everything in the code below works except for the binding on the ContextMenu. This is evidently due to the fact that the ContextMenu is located inside of a Style, which puts it in a different namescope from the rest of the xaml. I am looking for a solution where I won't have to instantiate a ContextMenu in the code-behind, since the application where I have to apply the solution contains a very large ContextMenu with a lot of bindings.
There must be a way to accomplish this in xaml, otherwise it would seem like a serious oversight. Also note that I've already tried traversing the element tree using VisualTreeHelper and LogicalTreeHelper, but I wasn't able to find the ContextMenu from the root element of the Window (these classes evidently skipped over the interesting elements). Anyway, all of the code is below.
This can be pasted into a new WPF application in Visual Studio, and nothing is missing. Here's the code for App.xaml.cs (the xaml was left unchanged): using System. Windows; namespace WpfApplication1 { /// /// Interaction logic for App.
Xaml /// public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base. OnStartup(e); WindowV windowV = new WindowV(); WindowVM windowVM = new WindowVM(); windowV. DataContext = windowVM; windowV.Show(); } } } Here's the xaml for what was originally Window1: Here's the codebehind for what was originally Window1: using System.
Windows; using System.Windows. Markup; namespace WpfApplication1 { /// /// Interaction logic for Window1. Xaml /// public partial class WindowV : Window { public WindowV() { InitializeComponent(); } private void OnToggleLock(object sender, RoutedEventArgs e) { if (((WindowVM)(DataContext)).
IsLocked == true) ((WindowVM)(DataContext)). IsLocked = false; else ((WindowVM)(DataContext)). IsLocked = true; } } } A new class was added to the project called WindowVM.
Here's its code: using System.Collections. Generic; using System. ComponentModel; namespace WpfApplication1 { public class WindowVM : INotifyPropertyChanged { public string MenuItem1 { get { string str = "Menu item 1"; return str; } } public string MenuItem2 { get { string str = "Menu item 2"; return str; } } public string MenuItem3 { get { string str = "Menu item 3"; return str; } } public List LockedList { get { List list = new List(); list.
Add("This items control is currently locked. "); return list; } } public List RegularList { get { List list = new List(); list. Add("Item number 1.
"); list. Add("Item number 2. "); list.
Add("Item number 3. "); return list; } } private bool _isLocked; public bool IsLocked { get { return _isLocked; } set { if (_isLocked! = value) { _isLocked = value; OnPropertyChanged("IsLocked"); } } } public WindowVM() { IsLocked = false; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string PropertyName) { if (PropertyChanged!
= null) PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); } } } Any insight would be very appreciated. Thanks much! Andrew wpf xaml binding contextmenu link|improve this question asked Jun 6 '10 at 19:14Andrew367111 89% accept rate.
Edit: Okay, at this point, I'm even having difficulty with this in the code-behind. Any ideas? – Andrew Jun 7 '10 at 1:08 In the above question, I just realized that I did make one adjustment to the App.
Xaml file. Specifically, I removed the line containing the startup url. Other than that, the App.
Xaml file remained unchanged. Otherwise, these files can be pasted into a new WPF application in Visual Studio, and the problem with right-clicking will be evident when the application is run. – Andrew Jun 7 '10 at 13:41.
Okay - I am having a little trouble following what you are trying to accomplish - but try the following: ... And to make your code easier, you can then try: ... Problem was that you were binding to the UI element Window which does not have a property called MenuItem1 etc. The DataContext property has the data you are interested in.
I appreciate the response, but neither of those solutions seem to work. – Andrew Jun 7 '10 at 12:42 Running the second suggestion that you made produces the following line in the Output Window of Visual Studio: "System.Windows. Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.
Window', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'ContextMenu' (Name='contextMenu'); target property is 'DataContext' (type 'Object')". I think this is the problem... – Andrew Jun 7 '10 at 12:54.
Alright, this solution works: I changed the WindowV. Xaml and WindowV.xaml.cs files as follows. The following corrections fix the problem concerning namescope in xaml.
Here's the new WindowV. Xaml file: Here's the corresponding code-behind: using System. Windows; using System.Windows.
Markup; using System; namespace WpfApplication1 { /// /// Interaction logic for Window1. Xaml /// public partial class WindowV : Window { public WindowV() { InitializeComponent(); System.Windows.Controls. ContextMenu contextMenu = FindResource("myContextMenu") as System.Windows.Controls.
ContextMenu; NameScope. SetNameScope(contextMenu, NameScope. GetNameScope(this as DependencyObject)); contextMenu.
RegisterName("RootElement", this); } private void OnToggleLock(object sender, RoutedEventArgs e) { if (((WindowVM)(DataContext)). IsLocked == true) ((WindowVM)(DataContext)). IsLocked = false; else ((WindowVM)(DataContext)).
IsLocked = true; } } } Andrew.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.