Øyv*_*hen 5 .net c# user-controls caliburn.micro
我对Caliburn.Micro很陌生,所以我想这有一个简单的答案(或者至少我希望它有:))
我有一个ViewModel,其属性名为ConnectedSystem
,具有名为的子属性Name
。
在我看来,我有以下XAML(摘录):
<StackPanel Orientation="Horizontal">
<Label Content="Name:" />
<TextBlock x:Name="ConnectedSystem_Name" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
这样就可以正常工作,并且TextBlock
按预期显示名称。但是,ConnectedSystem具有大约10个应显示的属性,我不想在视图中将XAML复制粘贴10次以上。相反,我想将XAML提取为UserControl,在其中可以将LabelText和Text设置为属性。
我试过了,但是我不确定如何使Caliburn.Micro自动将ConnectedSystem_Name传递到我的UserControl中。
这可能比在这里使用UserControl更好,因此,问题基本上是:将共享的XAML作为自己的控件,并且仍然能够使用Caliburn.Micros绑定的最佳方法是什么。
您需要做的是ContentControl
在主视图中使用a 来显示ConnectedSystem
主视图模型的属性。通过使用a,ContentControl
您将被包括在视图模型绑定过程中,并且将应用视图模型绑定器规则。因此,您希望您的媒体资源(使用Caliburn的默认实现)为类型ConnectedSystemViewModel
且具有名为的视图ConnectedSystemView
。然后,在用于显示想要的父项的视图中ContentControl
,带有x:Name
的ConnectedSystem
(ConnectedSystemViewModel属性的名称。这将导致视图模型绑定程序将两者连接起来并完成其通常的工作。以下是一些代码以简化说明:
ConnectedSystemView.xaml(在指定ContentControl
为显示主视图模型的已连接系统属性的控件时,约定将使用的用户控件)
<UserControl x:Class="Sample.Views.ConnectedSystemView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Column="0"
Grid.Row="0">PropertyOne Label:</Label>
<TextBox x:Name="PropertyOne"
Grid.Column="1"
Grid.Row="0"></TextBox>
<TextBlock Grid.Column="0"
Grid.Row="1">PropertyTwo Label:</TextBlock>
<TextBox x:Name="PropertyTwo"
Grid.Column="1"
Grid.Row="1"></TextBox>
<!-- repeat the TextBlock, TextBox pair for the remaining
properties three through ten -->
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
ConnectedSystemViewModel.cs(主视图模型上ConnectedSystem属性的类型)
namespace Sample.ViewModels
{
public class ConnectedSystemViewModel : PropertyChangedBase
{
private string _propertyOne;
public string PropertyOne
{
get { return _propertyOne; }
set
{
_propertyOne = value;
NotifyOfPropertyChange(() => PropertyOne);
}
}
// these all need to be as above with NotifyPropertyChange,
// omitted for brevity.
public string PropertyTwo { get; set;}
public string PropertyThree { get; set;}
public string PropertyFour { get; set;}
public string PropertyFive { get; set;}
public string PropertySix { get; set;}
public string PropertySeven { get; set;}
public string PropertyEight { get; set;}
public string PropertyNine { get; set;}
public string PropertyTen { get; set;}
}
}
Run Code Online (Sandbox Code Playgroud)
并在您的主视图中定义一个ContentControl,它相对于类型的主视图模型属性命名 ConnectedSystemViewModel
<ContentControl x:Name="ConnectedSystem"></ContentControl>
Run Code Online (Sandbox Code Playgroud)
如果我理解正确的话,那么这应该是您所需要的所有默认的Caliburn.Micro约定。显然,您将向具有适当名称的10个ConnectedSystem
属性ConnectedSystemViewModel
和适当的控件添加名称,ConnectedSystemView
以完成实现。
这样,您只需在主视图中定义一个ContentControl
即可显示ConnectedSytem属性(而不是10个相同的自定义用户控件),并且约定将确定用于填充的用户控件的类型ContentControl
。
在ConnectedSystemView
将ContentControl
通过约定插入到主视图的content属性的内部,您具有要显示10个连接的系统属性的控件。
Caliburn.Micro 不能很好地处理将多个项目自动链接到单个控件的情况。但是,您不必依赖自动活页夹,您可以使用普通的旧 wpf 绑定来代替。
AUserControl
是去这里的路。在代码中您可以添加两个 DependencyProperties,LabelText
和Text
。
然后在 UserControl 的 XAML 中,绑定到新属性。
在使用此控件的地方,您现在可以在 XAML 中设置 LabelText 和 Text 值。因此,在主视图上添加控件,并将LabelText
和绑定Text
到 ViewModel 中的属性。