none
Wpf Combobox中嵌入Treeview的功能 RRS feed

  • 问题

  • <ComboBox    Name="cb1">         
    
                         <TreeView  SelectedValuePath="Header" Width="280"   Name="tv1">
                               <TreeViewItem Header="Employee1">
                                      <TreeViewItem Header="Jesper"/>
                                      <TreeViewItem Header="Aaberg"/>
                                      <TreeViewItem Header="12345"/>
                               </TreeViewItem>
                               <TreeViewItem Header="Employee2">
                                      <TreeViewItem Header="Dominik"/>
                                      <TreeViewItem Header="Paiha"/>
                                      <TreeViewItem Header="98765"/>
                               </TreeViewItem>
     
                               <TreeView.Triggers>
                               <EventTrigger RoutedEvent="TreeView.SelectedItemChanged">
                                      <EventTrigger.Actions>
                                             <BeginStoryboard>
                                                    <Storyboard Duration="0:0:0" Storyboard.TargetName="cb1" Storyboard.TargetProperty="Tag" >
                                                           <ObjectAnimationUsingKeyFrames>
                                                                  <DiscreteObjectKeyFrame KeyTime="0:0:0"  Value="{Binding ElementName=tv1, Path=SelectedValue}">
     
                                                                  </DiscreteObjectKeyFrame>
                                                           </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                             </BeginStoryboard>
                                      </EventTrigger.Actions>
                               </EventTrigger>
                         </TreeView.Triggers>
                        
           </TreeView>
           </ComboBox>
    <ContentPresenter
           Name="ContentSite"
           IsHitTestVisible="False"
           Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}"      
    Margin="3,3,23,3"
    VerticalAlignment="Center"
    HorizontalAlignment="Left" />

    我放在程序里运行点击树子节点 ComBox中的值就变成这样了,他把我的整个树都当做ComBox的值了,请问这个该怎么修改让ComBox的值为树的子节点?我要实现的就是点击树的末级节点,我的ComBox中就显示我我刚才点击的值,就像我点击Employee1节点下的Jesper子节点,那么我点击后ComBox的值就是Jesper。忘大侠指教!

    wy926

    2012年8月26日 7:07

答案

  • 你可以使用GroupStyle。

    前台:

                <ComboBox Name="comboBox">
                    <ComboBox.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}"/>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                        </GroupStyle>
                    </ComboBox.GroupStyle>
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

    后台:

                List<Item> items = new List<Item>();
                items.Add(new Item() { Name = "Item1", Category = "A" });
                items.Add(new Item() { Name = "Item2", Category = "A" });
                items.Add(new Item() { Name = "Item3", Category = "A" });
                items.Add(new Item() { Name = "Item4", Category = "B" });
                items.Add(new Item() { Name = "Item5", Category = "B" });
    
                ListCollectionView lcv = new ListCollectionView(items);
                lcv.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
    
                this.comboBox.ItemsSource = lcv;

    class Item是一个定义了Name和Category的类。


    Wanpeng wanpeng.ones@gmail.com

    2012年8月27日 5:00