0

I'm currently using the following code to load and eventually display xaml files off disk:

StreamReader mysr = new StreamReader(absFile);
Window window = XamlReader.Load(mysr.BaseStream) as Window;
window.DataContext = SomeViewModel;

I am then able to do bindings in for text and other elements by accessing the properties on the view model like this:

<Label Content="{Binding Path=SomeStringProperty}" ... />

I can even setup converters for things like loading images or changing booleans to visibility values. What I can't get working is a binding that causes a button click or a menu item selection to call code within view model (or any other class in my application for that matter). I'd like the MenuItem to look something like this:

<MenuItem Command="{Binding Path=ClickCommand}" CommandParameter="Some Parameter" Header="Some Menu Item" />

Where this menuItem would call "ClickCommand" in my code when selected. Assuming this is done through some ICommand implementation I'd also like the ability to implement the "CanExecute" function as well, but I'd live without that if need be.

I've searched for hours, tried everything imaginable and can't get anything to work. I either get incredibly obscure exceptions or totally silent failure (meaning nothing happens when I click the menu item). Can somebody provide an example with both the xaml code and backing C# classes that can do this?

Right now the only thing I can think to do is walk the window elements using a VisualTreeHelper or some such and manually add the command bindings in based on some Name property convention. I know this can be made to work as I did a quick prototype of it, but there has to be a better way.

Thanks Dave

DAufderh
  • 25
  • 3
  • Are you sure the `MenuItem` has the `DataContext` you think it has? Try `Command="{Binding ClickCommand, PresentationTraceSources.TraceLevel=High}"` and observe the VS Output pane for trace information. That's the first thing to try. – 15ee8f99-57ff-4f92-890c-b56153 Nov 30 '16 at 19:15
  • The DataContext is on the Window not the MenuItem. Because this is a dynamically loaded XAML I have no way of telling what's in it. However the point here is to enable access to certain properties (which I have working) and certain actions (which I don't have working) through bindings. I will try the PresentationTraceSources.TraceLevel=High and see what that turns up – DAufderh Dec 01 '16 at 03:17
  • What do you mean "Action"? How and where is ClickCommand defined? Anyway MenuItems aren't in the visual tree so they don't inherit the window's DataContext. This is likely to be the issue with that binding. – 15ee8f99-57ff-4f92-890c-b56153 Dec 01 '16 at 03:26
  • @EdPlunkett This is what I'm trying to figure out. I want to call a function within my app when a Button is clicked or a MenuItem is selected. I've been looking at this link: [MS Docs](https://msdn.microsoft.com/en-us/library/ms752070(v=vs.110).aspx) but it throws an exception when trying to setup the Window.CommandBinding. I presume I want either the Command or Click properties, but that's where I can't get the bindings to work. So far TraceLevel=High has not helped. – DAufderh Dec 01 '16 at 03:46
  • Please share your definition of ClickCommand. It should be a property of your viewmodel -- whatever SomeStringProperty is a property of. But ClickCommand must return an object that implements ICommand. – 15ee8f99-57ff-4f92-890c-b56153 Dec 01 '16 at 03:48
  • @EdPlunkett See the answer below. Your suggestion for setting the TraceLevel saved the day. – DAufderh Dec 01 '16 at 04:03
  • Nice. It's never what you think it is. – 15ee8f99-57ff-4f92-890c-b56153 Dec 01 '16 at 04:04

1 Answers1

1

Setting PresentationTraceSources.TraceLevel=High was the key... I had tried about 100 things previously and got nowhere. However with the TraceLevel set, I went back to this posts suggestion:

How to bind WPF button to a command in ViewModelBase?

With the TraceLevel set I could clearly see things working except that I was using an older xaml file on disk as a test. It was manually setting the DataContext for the MenuItem and thus overriding the one that contained the command I was trying to invoke.

Community
  • 1
  • 1
DAufderh
  • 25
  • 3