【问题标题】:Prism WPF Binding RegionManager.RegionName棱镜 WPF 绑定 RegionManager.RegionName
【发布时间】:2020-09-28 19:08:01
【问题描述】:

我有一个带有两个模块的 Prism 7 应用程序,称为 ModuleA 和 ModuleB。在我的主窗口中,如果单击“显示 A”按钮,我希望能够显示 ModuleA,如果单击“显示 B”按钮,则显示 ModuleB。我通过以下方式实现了该行为:

MainWindow.xaml

<Window x:Class="ModulesTest.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:prism="http://prismlibrary.com/"
    prism:ViewModelLocator.AutoWireViewModel="True"
    Title="{Binding Title}" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ContentControl prism:RegionManager.RegionName="{Binding Module}" />
    <StackPanel Grid.Row="1">
        <Button Command="{Binding ShowModuleCommand}"
                CommandParameter="A">
            Show A
        </Button>
        <Button Command="{Binding ShowModuleCommand}"
                CommandParameter="B">
            Show B
        </Button>
    </StackPanel>
    
</Grid>

MainWindowViewModel.cs

public class MainWindowViewModel : BindableBase
{
    private string _title = "Prism Application";
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    private string fieldName;
    public string Module
    {
        get { return fieldName; }
        set { SetProperty(ref fieldName, value); }
    }

    private DelegateCommand<string> _showModuleCommand;
    public DelegateCommand<string> ShowModuleCommand =>
        _showModuleCommand ?? (_showModuleCommand = new DelegateCommand<string>(ExecuteShowModuleCommand));

    void ExecuteShowModuleCommand(string module)
    {
        Module = "Module" + module;
    }

    public MainWindowViewModel()
    {
        Module = "ModuleA";
    }
}

问题是 RegionManager.RegionName 仍然是在 ViewModel 的构造函数中设置的“ModuleA”,并且在单击“显示 B”时不会更改。 RegionManager.RegionName 的绑定是设计不允许的还是我做错了?

这里也是回购的链接:https://github.com/moisejbraver/ModulesTest

【问题讨论】:

    标签: c# wpf prism


    【解决方案1】:

    区域是用户界面的结构部分。将区域名称分配给特定控件后,您不应重新分配区域名称。

    如果您需要在某个区域内导航,请考虑使用IRegionNavigationService...

    【讨论】:

    • 感谢您的意见!这是否意味着该区域不能在数据模板中使用?我的最终目标是拥有一个选项卡控件,其中选项卡内容取决于添加到项目集合中的项目(即模块)。
    • 您也可以将区域名称分配给TabControl...
    • 我的问题更多与能够动态分配区域名称有关。我问了一个单独的问题来更好地解释我的问题stackoverflow.com/questions/64114543/prism-wpf-dynamic-regions
    猜你喜欢
    • 1970-01-01
    • 2011-02-15
    • 2011-03-16
    • 2018-11-10
    • 2011-04-02
    • 2012-08-16
    • 2021-09-07
    • 2020-11-07
    • 2012-03-12
    相关资源
    最近更新 更多