在组合框内预选Treeview的值

时间:2012-08-20 16:22:21

标签: c# wpf combobox treeview

我希望在组合框内实现树视图。基本上我希望它在折叠时显示为组合框,但在扩展时在组合框内显示树视图。当用户点击某个节点时,我希望它在折叠的组合框中显示。到目前为止,我已经完成了这项工作。

我遇到的问题是如何在加载此组合框时显示c#的默认值。请帮助我们,因为我的想法已经用完了:))

提前致谢。

数据模板                                   
                                                                                                              

    <DataTemplate x:Key="TreeViewExpanded" >

        <StackPanel>
            <TreeView x:Name="DPointTree" Margin="5" ItemsSource="{Binding Datapoint}" 
                              Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ComboBox}}}"
                              >
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Field}">
                        <TextBlock Text="{Binding Name}"/>
                                <HierarchicalDataTemplate.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding}"/>
                                    </DataTemplate>
                                </HierarchicalDataTemplate.ItemTemplate>
                            </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
        </StackPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding = "{Binding Path=SelectedFieldName, Mode=TwoWay}" Value = "">
                <Setter Property = "Visibility"  Value = "Collapsed"/>
            </DataTrigger>
        </DataTemplate.Triggers>
</DataTemplate>

这是Xaml

 <ComboBox Name="cmbFieldName" Width="150" Background="White" ItemsSource="{Binding}" SelectedItem="{Binding SelectedFieldName , Mode=TwoWay}" >
            <ComboBox.ItemTemplateSelector>
                <local:TreeViewSelector/>
            </ComboBox.ItemTemplateSelector>
        </ComboBox>

这是传递给它的DataSet。

Datapoint _datapoint2 = new Datapoint();
        _datapoint2.Name = "Alpha";
        _datapoint2.FieldID.Add("Contains Elements");
        _datapoint2.Field.Add("1 Year");
        _datapoint2.Field.Add("2 Year");
        _datapoint2.Field.Add("3 Year");

2 个答案:

答案 0 :(得分:0)

它不漂亮,但可以选择树中的节点

// in code behind after the tree is declared, e.g. after InitializeComponent();
SynchronizationContext.Current.Post(delegate
{
    // expand the tree to the selected node after loading
    treeView.ExpandAll(); // or if path is known treeView.ExpandPath(...);
    SynchronizationContext.Current.Post(delegate
    {
        treeView.GetContainerFromItem(root.Children[0]).IsSelected = true;
    }, null);
}, null);

答案 1 :(得分:0)

试试这个:

private void TreeView_Loaded(object sender, RoutedEventArgs e)
        {
            TreeView areaTreeView = sender as TreeView;

            // Expand the treeview
            if (areaTreeView != null)
            {
                ExpandCollapseTreeNodes(areaTreeView, true);
            }
        }



public static void ExpandCollapseTreeNodes(ItemsControl parentContainer, bool isExpanded)
        {
            foreach (Object item in parentContainer.Items)
            {
                TreeViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                if (currentContainer != null) // && currentContainer.Items.Count > 0
                {
                    //expand the item
                    currentContainer.IsExpanded = isExpanded;                    

                    //if the item's children are not generated, they must be expanded
                    if (isExpanded && currentContainer.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
                    {
                        //store the event handler in a variable so we can remove it (in the handler itself)
                        EventHandler eh = null;
                        eh = new EventHandler(delegate
                        {
                            //once the children have been generated, expand those children's children then remove the event handler
                            if (currentContainer.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
                            {
                                ExpandCollapseTreeNodes(currentContainer, isExpanded);
                                currentContainer.ItemContainerGenerator.StatusChanged -= eh;
                            }
                        });

                        currentContainer.ItemContainerGenerator.StatusChanged += eh;
                    }
                    //otherwise the children have already been generated, so we can now expand those children
                    else
                    {
                        ExpandCollapseTreeNodes(currentContainer, isExpanded);
                    }
                }
            }
        }