我已经使用MVVM模式创建了一个WPF桌面应用程序。作为这项工作的一部分,我创建了一个包含许多对话窗口的命名空间,dialogViewModels和dialogViews。对话框窗口通常显示其中一个dialogViews,具体取决于所分配的dialogViewModel。我想重用一个WPF UserControls库 - 如何设置样式?
我现在想要这个命名空间中的意见和的ViewModels转换为一个单独的库,这样我就可以重新使用在其他应用程序的对话框。但是,我有两个问题:
- 如何在我的usercontrols上设置样式,以便在不同的应用程序中使用库时,它会使用该应用程序中的样式。
- 重新使用库时,我能否覆盖库控件中的数据模板赋值?
一些代码来说明我的观点:
<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
。这些样式目前在我的App.xaml文件中的<Application.Resources>
下定义。如果我把对话在图书馆,我引用该库从一个新的应用程序,我如何才能获得使用StandardBorderStyle
和StandardButtonStyle
对话框从新的应用程序,这样每个应用程序可以定义它自己的视觉风格?
问题2是关于DataTemplates
。这些模板用于根据分配给Dialog的DataContext的ViewModel的类型,为对话框(在上例中插入为ContentControl
元素)选择适当的视图。在我想使用与不同的查看方式时,我能否在重新使用库时覆盖上面的DataTemplate?
哦,我知道这是两个问题,但你仍然只会得到名声一个答案,对不起! ;-)