2011-11-18 77 views
0

根據列表框的選擇,哪種做法是最佳方法來更換內容?想想win7的控制面板在左邊的鏈接是如何工作的 - 這就是我想要實現的。交換主要應用程序內容

到目前爲止,我已經設置了代碼,但由於某種原因,我無法將其實際工作(大概我錯誤地進行了綁定),我相信這可能不是最好的也可以採用。

<Window x:Class="ControlCenter.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"> 
<Window.Resources> 
    <ControlTemplate x:Key="workspace_1" x:Name="Workspace_1"> 
     <StackPanel> 
      <Button>Test</Button> 
      <Button>Link to Workspace 2</Button> 
      <Button>Random function 3</Button> 
     </StackPanel> 
    </ControlTemplate> 
    <ControlTemplate x:Key="workspace_2" x:Name="Workspace_2"> 
     <StackPanel> 
      <Button>Test 2</Button> 
      <TextBlock>Some random text</TextBlock> 
      <Button>Placeholder</Button> 
     </StackPanel> 
    </ControlTemplate> 
    <ControlTemplate x:Key="workspace_3" x:Name="Workspace_3"> 
     <Border Background="Black" /> 
    </ControlTemplate> 
</Window.Resources> 
<Grid> 
    <DockPanel> 
     <ListBox Name="lst_workspaces" Width="150"> 
      <ListBoxItem Content="{DynamicResource ResourceKey=workspace_1}" /> 
      <ListBoxItem Content="{DynamicResource ResourceKey=workspace_2}" /> 
      <ListBoxItem Content="{DynamicResource ResourceKey=workspace_3}" /> 
     </ListBox> 
     <ContentControl Template="{Binding ElementName=lst_workspaces, Path=SelectedItem.Value}"> 

     </ContentControl> 
    </DockPanel> 
</Grid> 

有什麼建議?

+0

它**將**非常感激,如果你因爲你沒有指出我犯的錯誤,所以給了我**的任何**反饋**。除此之外,我**已經**來這裏要求**答案**。問題不夠清楚嗎?它不夠簡潔嗎? – Johnny

回答

1

我相信你需要基於選擇在其他控制像ListViewListBoxTreeView

改變Conetnt上ContentControl我做了這樣的

  <ContentControl Name="userControlContentControl" 
          Content="{Binding ElementName=YourListViewname, 
               Path=SelectedItem}"> 
       <ContentControl.Resources> 
        <DataTemplate DataType="{x:Type ViewModelLayer:UserControl1ViewModel}"> 
         <ViewLayer:UserControl1 DataContext={Binding}/> 
        </DataTemplate> 
        <DataTemplate DataType="{x:Type ViewModelLayer:UserControl2ViewModel}"> 
         <ViewLayer:UserControl2 DataContext={Binding} /> 
        </DataTemplate> 
        <DataTemplate DataType="{x:Type ViewModellayer:UserControl3ViewModel}"> 
         <ViewLayer:UserControl3 DataContext={Binding} /> 
        </DataTemplate> 
        <DataTemplate DataType="{x:Type ViewModellayer:UserControl4ViewModel}"> 
         <ViewLayer:UserControl4 DataContext={Binding} /> 
        </DataTemplate> 
       </ContentControl.Resources> 
      </ContentControl> 

我的ListView的收藏ObservableCollection<ViewModelBase>是有在XAML中提到的所有不同ViewModel的實例

這裏告訴數據模板什麼類型的數據類型您選擇的項目將有...
DataTempaltes將自動選擇適合您的....正確的視圖在正確的數據tempalte ...;)

+0

聽起來像一個想法,但我仍然努力獲得它的一個很好的把握... ViewLayer應該是一個自定義名稱空間包含那些作爲自定義控制模板或? – Johnny

+0

不控制模板DataTemplates ..... ControlTemplate用於chnaging一個控件的外觀....一個DataTemplate將有一個用戶控件,將顯示所需的數據 – Ankesh

+0

@Johnny看到更新的答案...它可能會清除一些事情 – Ankesh