如何重用WPF视图? [英] How to reuse a wpf view?
问题描述
是否可以通过Caliburn.Micro重复使用WPF视图?
Is there a way to reuse a WPF View using it with Caliburn.Micro?
例如,我有一个带有SaveAndClose和Cancel按钮的DetailViewBase.xaml。 。现在,我想在PersonView(Model)中使用DetailViewModelBase和XAML。
In example I've got a DetailViewBase.xaml with a SaveAndClose and Cancel button. Now I want to use the DetailViewModelBase and the XAML in PersonView(Model).
我希望这个问题很清楚。
I hope this is question is clear.
推荐答案
如果我的假设是正确的,我想你的意思是你想从几个简单的视图模型组成一个更复杂的视图。您可以从viewmodels继承,但是显然一个viewmodel一次只能有一个活动视图。
If my assumption is correct, I think what you mean is that you want to 'compose' a more complex view from several simpler viewmodels. You can inherit from viewmodels, but obviously a viewmodel can only have one active view at a time.
但是,使用绑定可以组合多个viewmodel并使它们全部呈现。各个视图共同构成一个更复杂的UI
However, using bindings you can compose multiple viewmodels and have them all render their respective views together to make a more complex UI
例如
带有保存/取消按钮的VM
A VM with save/cancel buttons
public class ActionsViewModel
{
public void Save()
{
}
public void Cancel()
{
}
}
相应的视图:
<UserControl>
<StackPanel Orientation="Horizontal">
<Button x:Name="Save">Save</Button>
<Button x:Name="Cancel">Cancel</Button>
</StackPanel>
</UserControl>
另一个由自身和 ActionsViewModel
组成的视图
Another view which composes itself and the ActionsViewModel
public class ComposedViewModel
{
public ActionsViewModel ActionsView
{
get; set;
}
public ComposedViewModel()
{
ActionsView = new ActionsViewModel();
}
public void DoSomething()
{
}
}
视图:
<UserControl>
<StackPanel Orientation="Horizontal">
<TextBlock>Hello World</TextBlock>
<Button x:Name="DoSomething">Do Something</Button>
<ContentControl x:Name="ActionsView" />
</StackPanel>
</UserControl>
使用约定绑定(或使用附加属性)绑定 ContentControl
CM将自动将VM绑定到 DataContext
并连接视图。这样,您可以将多个VM组合成一个更复杂的功能。
When you use the convention binding (or use the attached properties) to bind a ContentControl
CM will automatically bind the VM to the DataContext
and wire up the view. This way you can compose multiple VMs into a single more complex piece of functionality.
此外,由于操作消息冒泡-如果要删除确定并在 ActionsViewModel
上执行取消,并将其放入 ComposedViewModel
实现中,操作消息将冒泡直至 ComposedViewModel
实例并在其上触发方法
Additionally, because of the action message bubbling - if you were to remove the 'Ok' and 'Cancel' implementation on the ActionsViewModel
and put them in the ComposedViewModel
implementation, the action message will bubble up to the ComposedViewModel
instance and fire the methods on there instead
例如
public class ActionsViewModel
{
// Remove the command handlers
}
public class ComposedViewModel
{
public ActionsViewModel ActionsView
{
get; set;
}
public ComposedViewModel()
{
ActionsView = new ActionsViewModel();
}
public void DoSomething()
{
}
// CM will bubble the messages up so that these methods handle them when the user clicks on the buttons in the ActionsView
public void Save()
{
}
public void Cancel()
{
}
}
编辑:
对不起,我忘记了这一点-基于约定的绑定将不允许消息冒泡,但您可以使用 Message.Attach
附加属性来使它起作用:
Sorry I forgot about this - convention based bindings won't allow messages to bubble, but you can just use the Message.Attach
attached property for this to work:
// Make sure you include the caliburn namespace:
<UserControl xmlns:cal="http://www.caliburnproject.org">
<StackPanel Orientation="Horizontal">
<Button x:Name="Save" cal:Message.Attach="Save">Save</Button>
<Button x:Name="Cancel" cal:Message.Attach="Cancel">Cancel</Button>
</StackPanel>
</UserControl>
这篇关于如何重用WPF视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!