C#Caliburn Micro管理窗口/视图 [英] C# Caliburn Micro managing windows/views
问题描述
我目前正在用C#开发WPF应用程序.我正在使用Caliburn Micro作为Framework.MainWindow(ShellView)主要由左部分和右部分组成.这两个部分都是TabControls.左侧有3个标签,右侧有6个标签.
I am currently developing a WPF App in C#. I am using Caliburn Micro as Framework. The MainWindow (ShellView) consists mainly of a left and a right part. Both parts are TabControls. On the left side I have 3 tabs and on the right side I have 6 tabs.
当前,我正在使用ShellViewModel的构造函数实例化9个视图的9个ViewModel,然后将具有ContentControl的View绑定到TabControl.
Currently I'm instantiating the 9 ViewModels of the 9 views with the constructor of the ShellViewModel, and then I bind the View with a ContentControl to the TabControl.
通常使用Caliburn Micro,可以使用Conductor-class更好地处理.我可以创建一个列表,然后将该列表绑定到TabControl上.
Normally with Caliburn Micro that could be handled better with the Conductor-class. I could create a list and bind that list to the TabControl and that's it.
但是问题是我只有一次Items-Collection,因为它是从Conductor-class继承的.因此,如果我将左侧的TabControl与右侧的TabControl绑定到相同的项,那么我在两侧都将具有相同的选项卡,这不是我想要的.
But the problem is I have the Items-Collection only once, because it's inherited from the Conductor-class. So if I bind the left TabControl to the same Items as the right TabControl I will have the same tabs on both sides, which is not what I want to have.
有没有办法拥有2个独立的Items-collection?
Is there a way to have 2 independent Items-collections?
谢谢
推荐答案
您需要为主屏幕的每一侧创建一个单独的视图模型.然后,每个视图模型都可以从 Conductor< object> .Collection.OneActive
类继承.然后,您的外壳视图模型将由两个子视图模型组成.您的外壳视图还将由两个内容控件组成,分别在其中显示每个子视图模型和子视图.
You need to create a separate view model for each side of your main screen. Each of these view models can then inherit from the Conductor<object>.Collection.OneActive
class. Your shell view model would then be composed of two sub-view models. Your shell view would also be composed of two content controls where each of the sub-view models and sub views are to be displayed.
public class ShellViewModel : Conductor<object>.Collection.AllActive {
public LeftSideViewModel LeftSide { get; set; }
public RightSideViewModel RightSide { get; set; }
public ShellViewModel(LeftSideViewModel leftSide, RightSideViewModel rightSide) {
LeftSide = leftSide;
RightSide = rightSide;
ActivateItem(leftSide);
ActivateItem(rightSide);
}
}
<UserControl x:Class="Workbench.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel LastChildFill="True">
<ContentControl x:Name="LeftSide" DockPanel.Dock="Top"/>
<ContentControl x:Name="RightSide"/>
</DockPanel>
</UserControl>
public class LeftSideViewModel : Conductor<object>.Collection.OneActive {
public LeftSideViewModel(...) {
ActivateItem(yourTabViewModel);
ActivateItem(anotherTabViewModel);
...
}
}
public class RightSideViewModel : Conductor<object>.Collection.OneActive {
public RightSideViewModel(...) {
ActivateItem(yourTabViewModel);
ActivateItem(anotherTabViewModel);
...
}
}
<UserControl x:Class="Workbench.Views.LeftSideView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TabControl x:Name="Items" BorderThickness="0" TabStripPlacement="Bottom">
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding TabText}" />
<!-- The grid will not appear (collapsed) if the tab cannot be closed -->
<Grid Visibility="{Binding CloseTabIsVisible, Converter={StaticResource BoolToVisibilityConverter}}">
<!-- The tab close button -->
<Button x:Name="CloseTab" Cursor="Hand" Focusable="False" Content="X" cal:Message.Attach="[Click] = [CloseTab($this)]" />
</Grid>
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
</Grid>
</UserControl>
这篇关于C#Caliburn Micro管理窗口/视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!