Sample code seems to be incomplete (Bars - Getting Started Series - Step 9)

Bars for WPF Forum

The latest build of this product (v25.1.1) was released 2 months ago, which was before this thread was created.
Posted 6 days ago by bcs99
Version: 25.1.1
Avatar

I'm trying to learn how to use Bars library using the Getting Started Series (this has been very helpful).  Following the code changes in Steps 1 - 8, I have been able to follow the examples and see them work as expected.  However, when I ran the Step 9 example, I expected the Content area of the Backstage to be populated using the HomeTemplate and NewTemplate assigned to the SampleBackstageTabContentTemplateSelector.  But, this is not happening.  I've tried to resolve it on my own and have not been able to work it out.

What steps to connect the Backstage content to the templates are missing?

Thanks,

Bob

[Modified 6 days ago]

Comments (3)

Answer - Posted 6 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Bob,

We're glad to hear you have found the Getting Started Series helpful, and so thankful you brought the issue with Step 9 to our attention! There was a change in our v25.1.0 release that broke that step, and we didn't catch it.

The RibbonBackstageTabViewModel class has a Content property that (in combination with ContentTemplate and ContentTemplateSelector) can be used to define the content of the tab.  That's what changed in v25.1.0.  Step 9 was initializing those view models, but did NOT initialize the new Content property to be an instance of the view model itself.  That is important because we want the view model instance to be passed to SampleBackstageTabContentTemplateSelector in order to return the correct view for the view model.

We originally discussed making the RibbonBackstageTabViewModel.Content property default to an instance of the view model, but decided against it since it meant somoneone wanting to just set the ContentTemplate property would also have to clear the Content property of the default value.  Unfortunately, the side effect of it not defaulting is exactly what you saw in the broken sample.

For the next release, we're going to reverse that decision and make it the default since it improves the out-of-the-box experience.

If you make the following changes to RibbonBackstageTabViewModel, the sample should work as expected.  Just update the one constructor of RibbonBackstageTabViewModel to also set the Content property as follows:

public RibbonBackstageTabViewModel(string key, string label, string keyTipText)
    : base(key) {

    // It is critical that the content of this view model is initialized to
    // itself so that the DataTemplateSelector assigned to RibbonBackstage.ContentTemplateSelector
    // can use the view model instance to select the appropriate template.
    Content = this;

    this.label = label ?? BarControlService.LabelGenerator.FromKey(key);
    this.keyTipText = keyTipText ?? BarControlService.KeyTipTextGenerator.FromLabel(this.label);
}

Please give that a shot and let us know if you experience any other issues with the sample.


Actipro Software Support

Posted 5 days ago by bcs99
Avatar

Thanks or your quick response.  However, that's not possible without the source.  My solution was to initialize the Content value of all of the  RibbonBackstageTabViewModel backstage items in the MainWindow constructor just before the template selector was set.  

foreach (var tabViewModel in viewModel.Ribbon.Backstage.Items.OfType<RibbonBackstageTabViewModel>())
    tabViewModel.Content = tabViewModel;
Posted 5 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Sorry for the confusion.  The Bars MVVM library is part of our open source repository, but I realize changing the source isn't easy.  What you did is a perfectly valid solution until the updates to the MVVM library are available.  Glad you got it working!


Actipro Software Support

Add Comment

Please log in to a validated account to post comments.