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 dyn开发者_如何学JAVAamic" FontWeight="SemiBold" Grid.ColumnSpan="3" Grid.Row="0" Grid.Column="2" BorderThickness="0" Background="{StaticResource DetailHeaderBackground}" ></sdk:Label>
Thank you!
Using MVVM you would wire up the Label to the underlying ViewModel, then just update the property when you change views:
<sdk:Label
Content="{Binding ViewModel.HeaderBarLabelText, Mode=OneWay}"
FontWeight="SemiBold"
Grid.ColumnSpan="3"
Grid.Row="0"
Grid.Column="2"
BorderThickness="0"
Background="{StaticResource DetailHeaderBackground}" >
</sdk:Label>
Then in the underlying model you have
[ViewModelProperty(null)]
public int HeaderBarLabelText
{
get
{
return _headerBarLabelText;
}
set
{
_headerBarLabelText= value;
OnPropertyChanged(() => HeaderBarLabelText);
}
}
It gets more complicated if your "content regions" / "views" are Prism modules, in which case the Prism Tutorial on CodeProject covers most bases.
http://www.codeproject.com/KB/silverlight/PrismTutorial_Part1.aspx
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.
精彩评论