这篇文章来自CodeProject,展示了一个WPF对话框控件,恰好符合您的需求。这种实现的原因是你不能把一个窗口放在任何其他控件的可视树中。也就是说,开箱即用的WPF不允许你在窗口内创建对话框。所以上述文章创建了一个子类ContentControl,它创建了一个窗口。
总之,你可以把它放在你的FooBarList视图里。
<dialog:Dialog Content="{Binding Path=DialogViewModel}" />
请确保您在资源字典中有类似以下内容的条目:
<Style TargetType="{x:Type dialog:Dialog}">
<Style.Triggers>
<Trigger Property="HasContent" Value="True">
<Setter Property="Showing" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
只需要像这样写(为了使 WPF 工作,您需要实现 INotifyPropertyChanged 接口):
public Class FooBarListViewModel
{
IList<FooBar> FooBarList {get;set;}
FooBar SelectedFooBar {get;set;}
ViewModelBase DialogViewModel {get;set;}
public EditFooBar(object param)
{
DialogViewModel = FooBar;
}
}
要将视图链接到编辑FooBar的FooBar ViewModel,只需像这样做(最好在Application.Resources中,这样它就是全局的)
<DataTemplate DataType={x:Type vm:FooBarViewModel}>
<vw:FooBarView/>
</DataTemplate>
(或者选择:使用IValueConverter将ViewModel转换为View,就像这篇文章所展示的那样)
然后你就可以开始了。听起来可能有点麻烦,但它确实能够大大解放你。