继承或封装View/Viewmodel
我想将一个视图/视图模型封装到另一个视图/视图模型中。目的是拥有一个弹出窗口、滑动面板或任何您能想到的常见行为,并在其中插入自定义视图/视图模型。
我想使用泛型来做到这一点,但我有点卡住了
父视图模型将看起来像那样
public class SidePanelViewModel<T>
{
public SidePanelViewModel(T enclosedViewModel)
{
EnclosedViewModel = enclosedViewModel;
}
public T EnclosedViewModel { get; private set; }
}
父视图将像那样
... whatever design and behavior ...
<ContentPresenter Content="{Binding EnclosedViewModel}"/>
... whatever design and behavior ...
在我的映射文件中我想将其放入,但问题是:
<DataTemplate DataType="{x:Type WPFTests:SidePanelViewModel}">
<WPFTests:SidePanelView />
</DataTemplate>
<DataTemplate DataType="{x:Type WPFTests:EnclosedViewModel}">
<WPFTests:EnclosedView />
</DataTemplate>
这不之所以有效,是因为视图模型 SidePanelViewModel 的构造函数需要创建一个类型。
我想我想做的是让一个视图/视图模型从另一个视图/视图模型继承
希望有人可以提供帮助
I want to encapsulate a view/viewmodel into another view/viewmodel. The aim is to have a popup, slide panel or whatever you can think of common behaviour and insert a custom view/viewmodel into it.
I want to use the generic for doing that but I'm kind of stuck
the parent view model will look like that
public class SidePanelViewModel<T>
{
public SidePanelViewModel(T enclosedViewModel)
{
EnclosedViewModel = enclosedViewModel;
}
public T EnclosedViewModel { get; private set; }
}
The parent view will be like that
... whatever design and behavior ...
<ContentPresenter Content="{Binding EnclosedViewModel}"/>
... whatever design and behavior ...
In my mapping file I would like to put that but here is the problem:
<DataTemplate DataType="{x:Type WPFTests:SidePanelViewModel}">
<WPFTests:SidePanelView />
</DataTemplate>
<DataTemplate DataType="{x:Type WPFTests:EnclosedViewModel}">
<WPFTests:EnclosedView />
</DataTemplate>
This does not work because the constructor of the viewmodel SidePanelViewModel requiere a type to be created.
I guess what I want to do is having a view/viewmodel inherit from another view/viewmodel
Hope someone can help on that
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么需要使用
?难道不能只使用常规
对象
或某种共享基础,例如IViewModel
或ViewModelBase
吗?Why do you need to use
<T>
?Can't you just use a regular
object
or some kind of shared base such asIViewModel
orViewModelBase
?在 WPF 中使用泛型类有时会非常痛苦。
我建议您创建一个基(抽象)类来实现您的(当然是虚拟的)方法的默认行为。
Working with generic classes in WPF can be quite painful sometimes ..
I would recommend you to create a base (abstract) class that implements the default behavior for your (virtual, of course) methods.