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