2012-04-12 117 views
1

我有一個TreeView的節點,所以當我點擊其中一個節點時,我將更新列表框的項目綁定到一個Observable集合,這很好。接下來,當我點擊樹視圖中的另一個節點時,我將不得不用不同的數據更新列表框(這也是一個Obsv.Collection)。任何想法如何進行這個?更新列表框項目

<Resources> 
    <DataTemplate x:Key="clipsource"> 
     <StackPanel> 
      <Image Name="img" Width="125" Height="70" Margin="1,0" Source="{Binding Path=Path, Converter= {x:Static l:UriToThumbnailConverter.Instance}}"/> 
      <TextBlock FlowDirection="LeftToRight" FontFamily="Arial" FontSize="12" Text="{Binding Path = DisplayClipName}"/> 
     </StackPanel> 
    </DataTemplate> 
</Resources> 
<Grid Height="Auto" Grid.Column="2" VerticalAlignment="Stretch" x:Name="RightPane_grid" Margin="2,0,0,0" KeyboardNavigation.DirectionalNavigation="None"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="21.205"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Rectangle Fill="#FF232323" Stroke="#FF000000" RadiusX="3" RadiusY="3" Margin="0,0,0,0" x:Name="Right_Pane_bkg" Grid.ColumnSpan="1" Grid.RowSpan="2" KeyboardNavigation.DirectionalNavigation="None"/> 
    <ListBox Name="ListBox1" SelectionMode="Extended" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="0" Background="#FF3B3B3B" Margin="2,25,2,2" ItemsSource="{Binding}" ItemTemplate="{StaticResource clipsource}"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel /> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 
</Grid> 

代碼背後:

private void Handle_Click1(object sender, MouseButtonEventArgs e) // tree view Item 
{ 
    ListBox1.DataContext = Clips Items; 
    // Clips Items is an Obs v. Collection 
} 

我有另一個觀測值ν,其我必須將它綁定到列表框中顯示另一種觀點認爲

+0

我看到你正在試圖粘貼XAML註釋。你可以編輯你的問題。把那裏 – 2012-04-12 20:26:15

+1

你可以顯示在第一個節點上點擊列表框的代碼嗎? – Steve 2012-04-12 20:27:54

+0

基本思路:將ListBox.ItemsSource綁定到TreeView的SelectedItem屬性。 – 2012-04-12 20:41:57

回答

0

你可以試試下面的收藏尚品:

<TreeView Name="MyTreeView"> 
     <TreeView.ItemContainerStyle> 
      <Style TargetType="{x:Type TreeViewItem}"> 
       <Setter Property="Header" Value="{Binding Path=Name}"/> 
       <Setter Property="ItemsSource" Value="{Binding Path=SomeItems}"/> 
      </Style> 
     </TreeView.ItemContainerStyle> 
    </TreeView> 

    <ListBox ItemsSource="{Binding ElementName=MyTreeView, Path=SelectedItem.SomeItems}"> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="Content" Value="{Binding Path=Name}"/> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 

綁定將從SelectedItem中檢索顯示在列表框中的項目在TreeView中使用Path SomeItems。這假設SelectedItem是這樣的:

public class MyTreeViewItem 
{ 

    public string Name { get; private set; } 
    public ObservableCollection<MyTreeViewItem> SomeItems { get; private set; } 

    public MyTreeViewItem(string name) 
    { 
     if (name == null) 
      throw new ArgumentNullException("name"); 

     this.SomeItems = new ObservableCollection<MyTreeViewItem>(); 
     this.Name = name; 
    } 

} 

當您在TreeView中選擇一個項目時,子項顯示在列表框中。

編輯:

要填充TreeView控件使用下面的代碼:

 MyTreeViewItem a = new MyTreeViewItem("A"); 
     a.SomeItems.Add(new MyTreeViewItem("A1")); 
     a.SomeItems.Add(new MyTreeViewItem("A2")); 
     a.SomeItems.Add(new MyTreeViewItem("A3")); 

     MyTreeViewItem b = new MyTreeViewItem("B"); 
     b.SomeItems.Add(new MyTreeViewItem("B1")); 
     b.SomeItems.Add(new MyTreeViewItem("B2")); 
     b.SomeItems.Add(new MyTreeViewItem("B3")); 

     this.MyTreeView.ItemsSource = new MyTreeViewItem[] { a, b };