本文介绍了Caliburn.Micro:如何将Conductor.Collection.AllActive的特定项绑定到ContentControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的目标是在ShellView的网格中显示4个不同的活动ViewModel。问题是我无法弄清楚如何将ContentControl连接到导体项目中的特定项目。他怎么办?My goal is to have 4 different active ViewModels displayed in a grid on the ShellView. The issue is that I have not been able to figure out how to wire up a ContentControl to a specific Item in Items of the Conductor. How can his be done?这是我正在尝试做的简化版本。Here is a simplified version of what I and trying to do. SolutionExplorer ShellViewModel:ShellViewModel:namespace ContentControlTest.ViewModels{ public class ShellViewModel : Conductor<object>.Collection.AllActive { public ShellViewModel() { ActivateItem(new UC1ViewModel()); ActivateItem(new UC2ViewModel()); ActivateItem(new UC3ViewModel()); ActivateItem(new UC4ViewModel()); } }} ShellView:ShellView:<Window x:Class="ContentControlTest.Views.ShellView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ContentControlTest.Views" xmlns:cal="http://www.caliburnproject.org" mc:Ignorable="d" Title="ShellView" Height="450" Width="800" > <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <ScrollViewer Grid.Row="0" Grid.Column="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <ContentControl cal:View.Model="{Binding UC1ViewModel}" cal:View.Context="{Binding Items[0]}"/> </ScrollViewer> <ScrollViewer Grid.Row="0" Grid.Column="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <ContentControl cal:View.Model="{Binding UC2ViewModel}" cal:View.Context="{Binding Items[1]}"/> </ScrollViewer> <ScrollViewer Grid.Row="1" Grid.Column="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <ContentControl cal:View.Model="{Binding UC3ViewModel}" cal:View.Context="{Binding Items[2]}"/> </ScrollViewer> <ScrollViewer Grid.Row="1" Grid.Column="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <ContentControl cal:View.Model="{Binding UC4ViewModel}" cal:View.Context="{Binding Items[3]}"/> </ScrollViewer> </Grid></Window>为简化起见,每个UserControl ViewModel和View都是相同的:For simplification each UserControl ViewModel and View are Identical: UC#ViewModel:UC#ViewModel:namespace ContentControlTest.ViewModels{ public class UC1ViewModel : Screen { private string id; public string ID { get { return id; } set { id = value; NotifyOfPropertyChange(() => ID); } } public UC1ViewModel() { ID = Guid.NewGuid().ToString(); } }} UC#视图:<UserControl x:Class="ContentControlTest.Views.UC1View" 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:local="clr-namespace:ContentControlTest.Views" xmlns:cal="http://www.caliburnproject.org" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" > <Border BorderBrush="Black" BorderThickness="1"> <StackPanel > <TextBlock Text="{Binding DisplayName}"/> <TextBlock Text="{Binding ID}"/> </StackPanel> </Border></UserControl>为了进行测试,我尝试使用ItemControl,它可以正常工作,但不能完全满足我的需求。For testing I have tried using an ItemControl and it works but doesn't give me exactly what I want.<ItemsControl x:Name="Items"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel></StackPanel> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl>推荐答案您需要在 ShellViewModel 类似于 UC1 , UC2 , UC3 等。然后,您需要更改您的 ShellView 绑定到 UC1 属性。You need to create properties in your ShellViewModel something like UC1, UC2, UC3 etc. You then need to change your ShellView to bind to UC1 property. <ContentControl x:Name="UC1" /> ... Caliburn Micro应该为您做管道工程。Caliburn Micro should do the plumbing for you.namespace ContentControlTest.ViewModels{ public class ShellViewModel : Conductor<object>.Collection.AllActive { // Modify to implement INotifyPropertyChanged event... public UC1ViewModel UC1 { get; set } public ShellViewModel() { UC1 = new UC1ViewModel(); ActivateItem(UC1); ActivateItem(new UC2ViewModel()); ActivateItem(new UC3ViewModel()); ActivateItem(new UC4ViewModel()); } }} 这篇关于Caliburn.Micro:如何将Conductor.Collection.AllActive的特定项绑定到ContentControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 16:32