2013-02-10 57 views
0

我想在我的主窗口中顯示一個視圖。如何添加視圖到我的主窗口

在過去,我已將我的View(類型UserControl)傳遞到我的MainWindow上的TabControl,並將其轉換爲TabItem,並且此工作正常。

在我的新應用程序中,我沒有使用TabControl,不幸的是,這是我知道如何將視圖插入到MainWindow中的唯一方法。我假設我現在可以使用ContentControl來顯示我的視圖。

我的問題是,我不知道如何將我的視圖綁定到我的ContentControl

我的XAML到目前爲止是相當光禿禿的,它看起來像

<Window x:Class="BackUps.Logging.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:viewModels="clr-namespace:BackUps.Logging" 
    Title="Logging Results" Height="350" Width="700"> 

<Grid> 
    <Grid.Resources> 
      <ContentControl 
        Content="{x:Type nameOfViewModel}" 
      /> 


    </Grid.Resources> 
</Grid> </Window> 

以上不工作,它好像我的做法,這是錯誤的,我在技術上很難對其進行編碼只允許1個視圖顯示。不過,就我的理解而言,這很好!

所以,我的2個問題是:

1)沒有我查看有哪些類型是(窗口,頁面或用戶控件,還是會以任何的工作,這些3) 2)如何設置ContentControl中綁定到我的視圖?

任何建議,將不勝感激。

回答

1

您的觀點應該是UserControl

在XAML中,你可以使用下面的代碼:

<Window x:Class="ContentBinding.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" 
     xmlns:views="clr-namespace:ContentBinding" 
     > 
    <Window.Resources> 
     <views:MyView x:Key="myView" /> 
    </Window.Resources> 
    <Grid> 
     <ContentControl Content="{StaticResource myView}" /> 
    </Grid> 
</Window> 
+0

啊,我是正確的,有過錯別的地方!注意自己,在學習時,添加一個固定的視覺控件(就像一個按鈕),以便它在屏幕上顯示,即使ViewModel中的綁定不好! D'哦!謝謝 – Dave 2013-02-11 08:48:05

相關問題