1

我用 Prism4 构建了一个 Silverlight 4 应用程序。我在 shell.xaml 中创建了几个内容区域,一切正常。现在我想做以下事情: 在 shell.xaml 中,布局中有一个标题栏(其中有一个标签,如下所示)。在那里,我们希望根据我的主要内容区域中显示的视图动态更改标题字符串的值。关于如何以简单的方式完成此任务的任何想法?

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

谢谢!

4

2 回答 2

0

由于我使用由 prism 模块的导出视图填充的 PRISM 区域,因此我现在这样做:

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);
        }
    }

我可以在通过 MEF 导入当前区域管理器的任何地方使用它。

于 2011-03-11T11:40:13.447 回答
0

使用 MVVM,您可以将 Label 连接到底层 ViewModel,然后在更改视图时更新属性:

<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>

然后在你的基础模型中

[ViewModelProperty(null)]
public int HeaderBarLabelText
{
    get
    {
        return _headerBarLabelText;
    }
    set
    {
        _headerBarLabelText= value;
        OnPropertyChanged(() => HeaderBarLabelText);
    }
}

如果您的“内容区域”/“视图”是 Prism 模块,它会变得更加复杂,在这种情况下,CodeProject 上的 Prism 教程涵盖了大多数基础。

http://www.codeproject.com/KB/silverlight/PrismTutorial_Part1.aspx

于 2011-03-10T12:11:50.913 回答