2011-11-18 80 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