2011-08-18 49 views
1

我已经使用MVVM模式创建了一个WPF桌面应用程序。作为这项工作的一部分,我创建了一个包含许多对话窗口的命名空间,dialogViewModels和dialogViews。对话框窗口通常显示其中一个dialogViews,具体取决于所分配的dialogViewModel。我想重用一个WPF UserControls库 - 如何设置样式?

我现在想要这个命名空间中的意见和的ViewModels转换为一个单独的库,这样我就可以重新使用在其他应用程序的对话框。但是,我有两个问题:

  1. 如何在我的usercontrols上设置样式,以便在不同的应用程序中使用库时,它会使用该应用程序中的样式。
  2. 重新使用库时,我能否覆盖库控件中的数据模板赋值?

一些代码来说明我的观点:

<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是关于使用StandardBorderStyleStandardButtonStyle。这些样式目前在我的App.xaml文件中的<Application.Resources>下定义。如果我把对话在图书馆,我引用该库从一个新的应用程序,我如何才能获得使用StandardBorderStyleStandardButtonStyle对话框从的应用程序,这样每个应用程序可以定义它自己的视觉风格?

问题2是关于DataTemplates。这些模板用于根据分配给Dialog的DataContext的ViewModel的类型,为对话框(在上例中插入为ContentControl元素)选择适当的视图。在我想使用与不同的查看方式时,我能否在重新使用库时覆盖上面的DataTemplate?

哦,我知道这是两个问题,但你仍然只会得到名声一个答案,对不起! ;-)

回答

0

对于样式,我能想到的最好方法是在您自己的UserControl中为每个样式定义一个DependencyProperty。例如,您将拥有一个名为BorderStyleDependencyProperty,您可以将默认设置为您在控件库中定义为internaly的样式,同时仍允许使用您的控件的客户端代码以他们想要的方式覆盖此样式。

对于DataTemplate,你不会真的做任何事情,因为你的库的用户可以定义一个又一个DataTemplate(例如用于MessageBoxDialogViewModel)在他的代码,这将优先考虑和用来代替的默认一个。

希望这会有所帮助:)

相关问题