Search code examples
silverlightprismprism-4

Silverlight 4 + Prism add dynamic string to a header bar in the shell


I have built a Silverlight 4 application with Prism4. I created several content regions in the shell.xaml and everything is working just fine. Now I want to do the following: In the shell.xaml we have a header bar (in there a label as shown below) in the layout. In there we want to dynamically change the value of the header string depending on the displayed view in my main content region. Any ideas of how I could accomplish this in an easy way?

<sdk:Label Content="Should be dynamic" FontWeight="SemiBold" Grid.ColumnSpan="3" Grid.Row="0" Grid.Column="2" BorderThickness="0" Background="{StaticResource DetailHeaderBackground}"  ></sdk:Label>

Thank you!


Solution

  • Since I use PRISM regions which are populated by exported views of a prism module I now did it like this:

    public static void AddLabelToHeaderRegion(string HeaderName, IRegionManager regionManager)
        {
            Label headerLabel = new Label
            {
                Content = HeaderName,
                FontWeight = System.Windows.FontWeights.SemiBold,
                Background = (System.Windows.Media.Brush)Application.Current.Resources["DetailHeaderBackground"],
                Padding = new Thickness(30, 3, 0, 3),
                BorderThickness = new Thickness(0),
                Margin = new Thickness(0)
    
            };
            Grid.SetColumn(headerLabel, 2);
            Grid.SetRow(headerLabel, 0);
            Grid.SetColumnSpan(headerLabel, 3);
            IRegion headerBarRegion = regionManager.Regions[RegionNames.HeaderBarRegion];
            if (headerBarRegion != null)
            {
                foreach (var item in headerBarRegion.ActiveViews)
                {
                    headerBarRegion.Remove(item);
                }
    
                headerBarRegion.Add(headerLabel);
            }
        }
    

    I can use this everywhere I import the current region manager via MEF.