【问题标题】:How to get the underlaying viewmodel of a view/region from another view in WPF Prism如何从 WPF Prism 中的另一个视图获取视图/区域的底层视图模型
【发布时间】:2014-10-07 13:29:52
【问题描述】:

我有 3 个区域,即菜单、工具栏和内容。在菜单视图中单击模块(即客户)时,它将导航到内容区域中的相应视图,在编辑文本框中的值后,我需要将其保存到数据库。在工具栏视图中单击保存按钮时,如何获取活动视图的底层视图模型并发送到 DAL? Shell.xaml:

<Grid DockPanel.Dock="Left" Width="65" Margin="1">
        <Border CornerRadius="1" BorderBrush="Black" BorderThickness="1">
            <StackPanel Orientation="Vertical" Margin="1" >
                <ContentControl Name="menuControl" PrismRegions:RegionManager.RegionName="{x:Static infra:RegionNames.MenuRegion}"/>
            </StackPanel>
        </Border>
    </Grid>
    <Grid DockPanel.Dock="Top" Margin="1">
        <Border CornerRadius="1" BorderBrush="Black" BorderThickness="1" >
            <StackPanel Orientation="Vertical" Margin="2">
                <ContentControl Name="toolbarControl" PrismRegions:RegionManager.RegionName="{x:Static infra:RegionNames.ToolbarRegion}"/>
            </StackPanel>
        </Border>
    </Grid>
    <Grid DockPanel.Dock="Right" Margin="2">
        <Border CornerRadius="1" BorderBrush="Black" BorderThickness="1">
    <StackPanel Margin="5" >
            <ContentControl Name="contentControl"  DockPanel.Dock="Top" PrismRegions:RegionManager.RegionName="{x:Static infra:RegionNames.ContentRegion}"/>
    </StackPanel>
        </Border>
    </Grid>

菜单.xaml:

<Button Grid.Row="0" Name="btnHome" Background="Transparent" Margin="1" ToolTip="Home" Command="{x:Static inf:ApplicationCommands.NavigationCommand}" CommandParameter="{x:Type mod:HomeView}">
        <TextBlock Height="32" Width="32" Text ="">
            <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/home_32.png"/>
            </TextBlock.Background>
        </TextBlock>
    </Button>
    <Button Grid.Row="1" Name="btnBank" Background="Transparent" Margin="1" ToolTip="Bank" Command="{x:Static inf:ApplicationCommands.NavigationCommand}" CommandParameter="{x:Type mod:BankView}">
        <TextBlock Height="32" Width="32" Text ="">
            <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/bank_64.png"/>
            </TextBlock.Background>
        </TextBlock>
    </Button>
    <Button Grid.Row="2" Name="btnCustomer" Background="Transparent" Margin="1" ToolTip="Customer" Command="{x:Static inf:ApplicationCommands.NavigationCommand}" CommandParameter="{x:Type mod:CustomerView}">
    <TextBlock Height="32" Width="32" Text ="">
        <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/customer_128.ico"/>
        </TextBlock.Background>
    </TextBlock>
    </Button>
    <Button Grid.Row="3" Name="btnEmployee" Background="Transparent" Margin="1" ToolTip="Employee" Command="{x:Static inf:ApplicationCommands.NavigationCommand}" CommandParameter="{x:Type mod:EmployeeView}">
    <TextBlock Height="32" Width="32" Text ="">
        <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/employee_128.png"/>
        </TextBlock.Background>
    </TextBlock>
    </Button>

工具栏.xaml:

<Button Grid.Column="0" Margin="1" Name="btnAdd" Height="35" Width="35"  ToolTip="Add" Background="Transparent" Command="{Binding AddCommand}" CommandParameter="Add" >
            <TextBlock Height="30" Width="30" Text ="">
                <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/plus_32.png"/>
                </TextBlock.Background>
            </TextBlock>
        </Button>
    <Button Grid.Column="1" Margin="1" Name="btnEdit" Height="35" Width="35" ToolTip="Edit" Background="Transparent" Command="{Binding EditCommand}" CommandParameter="Edit"  >
            <TextBlock Height="30" Width="30" Text ="">
                <TextBlock.Background>
                    <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/pencil_32.png"/>
                </TextBlock.Background>
            </TextBlock>
        </Button>
    <Button Grid.Column="2" Margin="1" Name="btnSave" Height="35" Width="35" ToolTip="Save" Background="Transparent" Command="{Binding SaveCommand}" CommandParameter="Save" >
            <TextBlock Height="30" Width="30" Text ="">
                <TextBlock.Background>
                    <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/save_32.png"/>
                </TextBlock.Background>
            </TextBlock>
        </Button>
    <Button Grid.Column="3" Margin="1" Name="btnPrint" Height="35" Width="35" ToolTip="Print" Background="Transparent" Command="{Binding PrintCommand}" CommandParameter="Print" >
            <TextBlock Height="30" Width="30" Text ="">
                <TextBlock.Background>
                    <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/print_32.png"/>
                </TextBlock.Background>
            </TextBlock>
        </Button>

界面设计:

【问题讨论】:

  • 你能发布你的内容区域视图代码和视图模型代码吗?

标签: wpf mvvm prism


【解决方案1】:

基本上,您的视图状态完全存在于视图模型中。在您的 Button 位于表单视图模型之外的场景中,您可以做的是:

在工具栏视图模型中: 引发点击保存按钮的事件

myEventAggregator.RaiseEvent<SaveButtonClicked>().Publish(args);

关于视图模型构造函数: 订阅您单击工具栏区域中的保存按钮时引发的事件

myEventAggregator.GetEvent<SaveButtonClicked>().Subscribe(yourSubscriberDelegate);

退出表格或取消表格: 取消订阅活动。 (这确保您没有多个活动表单同时保存数据)

myEventAggregator.GetEvent<SaveButtonClicked>().UnSubscribe(yourSubscriberDelegate);

为此,您需要有关发布和订阅的事件聚合器知识。了解更多详情here

【讨论】:

    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多