DataContext of ribbon Backstage gets set to null

Ribbon for WPF Forum

Posted 6 years ago by Ulrich Bürgi
Version: 13.1.0581
Platform: .NET 4.5
Environment: Windows 7 (64-bit)
Avatar

I have a ribbon backstage which uses data binding to to populate the properties of its backstage items. My view currently looks as follows:

<actiproControls:Backstage  x:Class="PrismApplicationPrototype.Modules.Backstage.BackstageView"
                    Name="Backstage"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:actiproControls="clr-namespace:ActiproSoftware.Windows.Controls.Ribbon.Controls;assembly=ActiproSoftware.Ribbon.Wpf"
                    xmlns:my="clr-namespace:PrismApplicationPrototype.Modules.Backstage"
                    mc:Ignorable="d" 
                    d:DataContext="{d:DesignInstance Type=my:BackstageViewModel}"
                    d:DesignHeight="300" d:DesignWidth="300"
                    >

    <actiproControls:BackstageTab Header="{Binding Test}"/>

    <actiproControls:Button Label="{Binding Test}"
                            Command="{Binding CloseApplicationCommand}"
                            KeyTipAccessText="X" />
 </actiproControls:Backstage>

 And the corresponding view model: 

public class BackstageViewModel
{
    private ICommand closeApplicationCommand;
    public ICommand CloseApplicationCommand
    {
        get { return this.closeApplicationCommand ?? (this.closeApplicationCommand = new DelegateCommand(CloseApplication)); }
    }

    private void CloseApplication()
    {
        System.Windows.Application.Current.Shutdown();
    }

    public string Test { 
        get { return "Test"; }
    }
}

 

Now, as soon as I click on the File button in the ribbon, which will show the Backstage, the Backstage's DataContext is changed and set to null. You can see this when subscribing the the Backstage's DataContextChanged event. Hence, in the running application the Backstage items have no labels or commands associated.

To test whether my bindings are OK I wrapped the Backstage into a Group inside of a Ribbon Tab. When doing this, the DataContext is not changed and the bindings work as expected. Of course this is not the proper place to put a Backstage...

// DataContext of the Backstage gets set to  null. Bindings break.
//ribbon.ApplicationMenu = backstage;

// DataContext is kept. Bindings work.
var tab = new Tab {Label = "Tab hosting Backstage"};
var tabGroup = new Group();
tabGroup.Items.Add(backstage);
tab.Items.Add(tabGroup);
ribbon.Tabs.Add(tab);

[Modified 6 years ago]

Comments (1)

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

Hi Ulrich,

The Backstage (when used from the File menu) is normally opened within an adorner on the root window.  So it's not within the visual tree hierarchy of the Ribbon.  Thus, we have logic in the code that creates Backstage to bind its DataContext to the Ribbon's DataContext.  So it should be getting whichever DataContext that Ribbon has.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.