DataContext Confusion

May 14, 2009 at 3:13 PM

Hi,

We have a scenario where we are creating our mainview in the startup of the app.xaml.cs. Then we create the viewmodel. wqe then set the DataContext on the mainview to the viewmodel. Pretty normal stuff. In my mainview I have a RibbonControl. If in the constructor for the mainview I new up the viewmodel and then set the mainview's DataContext my RibbonControl buttons are enabled (using the CanExecute, etc). But when I don't have this viewmodel created in the constructor the RibbonControl's buttons are all disabled. I created a delegate for the OnDataContextChanged event and I am getting that event firing when I set the DataContext. The RibbonControl is using ICommand for wiring up. Any ideas why this doesn't work?

thanks in advance!!

Bill

May 14, 2009 at 3:18 PM

Hi,

 

I just noticed that if I put the creating of the viewmodel and setting the DataContext in the constructor AFTER the InitializeComponent() call that the RibbonControl buttons are also disabled. Is there some way to get the RibbonControl to refresh?

thanks!

Bill

May 16, 2009 at 6:56 PM

Hi,

So - as I mentioned it looks that the DataContext must be set in the UserControl that has the RibbonControl in it in the constructor BEFORE the InitializeComponent() is called. Curious. I would think that you could set the DataContext at any time as it does cause a DataContextChanged event to occur. Shouldn't that wire things up?

In the app.xaml.cs file I would think that I could create the Window, create the ViewModel, and then set the DataContext of the Window (and by inheritance the UserControl's DataContext) and that should make everything work.

<font color="#0000ff">

public

</font>

void App_Startup(object sender, StartupEventArgs e)

{

<font color="#0000ff">

 

</font>

try

{

//Show the main window

PenskeRouteAssist vRAWindow = new PenskeRouteAssist();

vRAWindow.dockManager.ParentWindow = vRAWindow;

// Create the ViewModel to which the main window binds

var penskeRouteAssitViewModel = new PenskeRouteAssistViewModel(vRAWindow.dockManager);

vRAWindow.DataContext = penskeRouteAssitViewModel;

vRAWindow.Show();

}

catch (Exception ex)

...

Any ideas? I'm not sure if even if this works that the CanExecute() methods in the ViewModel that should enable and disable the Ribbon Buttons will work if the value changes from True to False.

thanks!!

Bill

May 27, 2009 at 1:04 PM

Hi,

Is there anyone on this list that knows about the RibbonControl? We have been struggling with this so much for a couple weeks now. We can't figure out how to have a change of state of CanExecute for RibbonButtons to cause them to activate/deactivate. We can't even figure out how to set the Ribbon Title during runtime. Yikes. Is there 'anywhere' we can get some help/ideas?

thanks,

Bill

May 27, 2009 at 10:12 PM

Hi. Did you try to find some posts about Ribbon here? I think that your can avoid these problems when application structure and DataContext managing are well considered. Look here: http://wpf.codeplex.com/Thread/View.aspx?ThreadId=52089 and here: http://wpf.codeplex.com/Thread/View.aspx?ThreadId=41623 for why and how to extend RibbonCommand. Hope be helpful.

May 28, 2009 at 9:10 PM

Firedog607,

Thanks!! I hadn't found those links when I had searched on this site. I am using the NonRoutedRibbonCommand as shown by Jaime Rodriquez in his Southridge Labs example. I'm still trying to figure out how to get the change of CanExecute's state to bubble up to the Ribbon. I'm sure I'm missing something.

Again, Thanks for the reply!

Bill