2013-08-27 37 views
0

我创建了一个BaseWindowView和我想在我的所有应用程序的基础要使用的自定义的框架BaseViewModel如何在应用程序中使用框架中的视图? (WPF MVVM)

BaseViewModel我有,例如,其允许我添加按钮的方法。我可以定义一个Command,一个ButtonImageLabelString。下面是这个方法的调用示例代码:

AddButton(OpenAnyTabCommand, "../Images/image.png", "LabelString"); 

在我BaseWindowView我有一个RibbonMenue,所有我在BaseViewModel定义了RibbonButtons都显示:

<ItemsControl x:Name="CButtons" ItemsSource="{Binding Buttons}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel x:Name="ButtonStackPanel" Orientation="Horizontal"> 
      </StackPanel> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <ribbon:RibbonButton 
       x:Uid="Button" 
       LargeImageSource="{Binding Path=ImageString}" 
       Label="{Binding Path=LabelString}" 
       Command="{Binding Path=ButtonCommand}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

我有这在一个示例项目中,并且工作正常。现在我想将BaseWindowViewBaseViewModel外包给一个框架并从那里使用它。

我的计划是:在我的每个应用程序中,我想将BaseWindowView设置为我的应用程序的MainWindow。在这个应用程序本身,我只想要一些UserControls这应该显示为我的BaseWindowView中的选项卡。我在BaseViewModel定义的按钮应该调用定义Command这将打开一个新的标签,并显示此Command背后的ViewModel

那么,什么是处理我的问题的最好方法是什么?我知道XAML中没有“经典继承”。我可以做一些像在框架视图中将中的StartUpUri这样的定义,还是“稍微有点棘手”?

作为补充:我发现我可以在App.xaml像下面的定义DataTemplate用于TabItems(其是UserControls):

<DataTemplate DataType="{x:Type ViewModel:AnyViewModel}"> 
    <view:TabItemAny/> 
</DataTemplate> 

@Sheridan:这里是关于BaseWindowView的问题。

+0

的问题不是很清楚:这是什么问题?哟意识到没有XAML继承这样的东西? –

+0

是的,我知道没有“经典继承”。我的问题是我如何做一些像视图的继承。我想在我的应用程序中显示我的'Framework-View'作为MainWindow,并且只需添加一些用于在此窗口中打开Tabs的UserControls。 – Tobias

+0

@Sheridan,这是问题 – Tobias

回答

1

好了,所以下手,让我们将使用BaseWindowViewMainWindow

首先,在App.xaml文件,删除的自定义ApplicationStartup性能StartupUri声明。然后在App.xaml.cs类,添加启动处理程序:

protected override void OnStartup(StartupEventArgs e) 
{ 
    base.OnStartup(e); 
    BaseWindowView baseWindowView = new BaseWindowView(); 
    baseWindowView.Show(); 
} 

这将打开你的Window,而不是MainWindow的。

现在到观的继承......如你所知,有在WPF没有这样的事情,但是有(种)是解决这个问题的方法。如果像你说的,所有的意见为主,那么我们就可以用ContentControl对象,以显示他们UserControl,前提是相关DataTemplate对象存在。

这基本上意味着如果你想使用特定的Window作为你的视图的外部模板,那么你可以添加一个ContentControl来显示视图。作为一个例子,我有一个动画Window,它显示为一个对话控件,带有按钮,我将几个不同的UserControl连接起来,这样它们都具有相同的整体外观。

在XAML:

... 
<Border CornerRadius="3.5" BorderBrush="{StaticResource TransparentBlack}" 
    BorderThickness="1" Padding="1"> 
    <ContentControl Content="{Binding ViewModel, RelativeSource={RelativeSource 
     FindAncestor, AncestorType={x:Type Controls:AnimationWindow}}}" /> 
</Border> 
... 

而且在后面的Window代码:

public static readonly DependencyProperty ViewModelProperty = DependencyProperty. 
    Register("ViewModel", typeof(BaseViewModel), typeof(AnimationWindow)); 

public BaseViewModel ViewModel 
{ 
    get { return (BaseViewModel)GetValue(ViewModelProperty); } 
    set { SetValue(ViewModelProperty, value); } 
} 

使用这个类,我可以绑定到XAML此ViewModel属性,或者从传递的值设置通过构造函数。由于我所有的视图模型都扩展了这个类,我可以将这个属性设置为其中的任何一个。

UPDATE >>>

使用ContentControlDataTemplate s到连接的意见/视图模型的一点是,我们从“硬编码的路程。我可以在每个应用程序中使用此设置,我不是而是与任何特定实现绑定在一起。

现在,如果你说你真的想使用相同视图/视图模型配对在您的所有不同的应用程序,那么你应该把你的DataTemplate s到的意见/视图模型连接在一个独立的Resources xaml文件。这样,你可以在这里你需要把这个文件合并到应用程序中的App.xamlResources部分而不是当你不:

<Application.Resources> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="ViewModelToViewDataTemplates.xaml"/> 
     <ResourceDictionary> 
      <!-- Add your normal resources here --> 
     </ResourceDictionary> 
    </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 
+0

谢谢你的回答!添加启动处理程序正常工作。你知道我是否可以在我的'BaseViewWindow'中添加一个UserControl(它应该是一个ComboBox)的调用,但UserControl本身是在我的应用程序中定义的?此外,UserControl的DataContext应该是我的应用程序中的'ViewModel'。 – Tobias

+0

嘿。 '你是什么意思'你知道我是否可以在我的'BaseViewWindow''中添加一个'UserControl'的调用(它应该是一个'ComboBox')? – Sheridan

+0

我想在我的'BaseViewWindow'中有''View:UserControlPerson x:Name =“UcPerson”DataContext =“{Binding Path = PersonViewModel}”/>'。但UserControl本身和PersonViewModel在应用程序中,而不在框架中。 – Tobias

相关问题