我使用MVVM模式创建了一个WPF桌面应用程序。作为这项工作的一部分,我创建了一个包含许多对话框窗口,dialogViewModels和dialogViews的命名空间。对话框窗口通常显示一个dialogViews,具体取决于指定的dialogViewModel。
我现在想要将此命名空间中的Views和ViewModel转换为单独的库,以便我可以在其他应用程序中重用这些对话框。但是,我有两个问题:
一些代码来说明我的观点:
<Window x:Class="FeehandlerMain.Dialogs.OKDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Path=DialogTitle}"
Height="200" Width="400" ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" WindowStartupLocation="CenterOwner" SizeToContent="Height">
<Window.Resources>
<DataTemplate DataType="{x:Type dialogs:MessageBoxDialogViewModel}">
<dialogs:MessageBoxDialogView />
</DataTemplate>
</Window.Resources>
<Border Style="{StaticResource StandardBorderStyle}" >
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl Content="{Binding}"
Grid.Column="0" Grid.Row="0" />
<StackPanel Orientation="Horizontal"
Grid.Column="0" Grid.Row="1">
<Button Content="OK" IsDefault="True" IsCancel="True"
Style="{StaticResource StandardButtonStyle}"/>
</StackPanel>
</Grid>
</Border>
</Window>
问题1 是关于StandardBorderStyle
和StandardButtonStyle
的使用。这些样式目前在<Application.Resources>
下的App.xaml文件中定义。如果我将对话框放在库中,并且我从新应用程序引用该库,如何让对话框使用 new 应用程序中的StandardBorderStyle
和StandardButtonStyle
,这样每个应用程序都可以定义它自己的视觉风格吗?
问题2 与DataTemplates
有关。这些模板用于根据分配为Dialog的DataContext的ViewModel的类型,为Dialog(在上面的示例中插入为ContentControl
元素)选择适当的视图。在重新使用库的情况下,我是否可以覆盖上面的DataTemplate,我希望MessageBoxDialogView
使用与MessageBoxDialogViewModel
不同的视图?
哦,我知道这是两个问题,但是你仍然只能获得一个答案的声誉,对不起! ; - )
答案 0 :(得分:0)
对于样式,我能想到的最好方法是在自己的DependencyProperty
中为每个样式定义UserControl
。例如,您将拥有一个名为DependencyProperty
的{{1}},您可以将默认设置为您在控件库中定义的样式,同时仍然允许使用您的控件的客户端代码覆盖此样式他们想要的方式。
对于BorderStyle
,您实际上不需要做任何事情,因为您的图书馆用户可以在他的代码中定义另一个DataTemplate
(例如DataTemplate
),这将优先使用而不是默认的。
希望这会有所帮助:)