none
silverlight2中如何实现 DropDownList+Tree? RRS feed

  • Frage

  •  

    silverlight2中如何实现 DropDownList+Tree的效果?我用combobox+treeview实现了效果,但是怎么也无法把tree的的值付给Combobox。求各位大牛帮帮忙!
    Donnerstag, 13. November 2008 02:28

Antworten

  • Code Snippet

    <UserControl x:Class="TreeviewAndCombobox.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <controls:TreeView Margin="5">
                <controls:TreeViewItem>
                    <controls:TreeViewItem.Header>
                        <Button Content="Button" />
                    </controls:TreeViewItem.Header>
                </controls:TreeViewItem>
                <controls:TreeViewItem>
                    <controls:TreeViewItem.Header>
                        <CheckBox Content="CheckBox" />
                    </controls:TreeViewItem.Header>
                </controls:TreeViewItem>
                <controls:TreeViewItem>
                    <controls:TreeViewItem.Header>
                        <TextBox Text="TextBox" />
                    </controls:TreeViewItem.Header>
                </controls:TreeViewItem>
                <controls:TreeViewItem>
                    <controls:TreeViewItem.Header>
                        <ComboBox x:Name="cbx" SelectionChanged="ComboBox_SelectionChanged">
                            <ComboBox.Items>
                                <TextBlock Text="111"/>
                                <TextBlock Text="222"/>
                                <TextBlock Text="333"/>
                            </ComboBox.Items>
                        </ComboBox>
                    </controls:TreeViewItem.Header>
                </controls:TreeViewItem>
            </controls:TreeView>
        </Grid>
    </UserControl>

     

     

    今天测了,可以啊,不过要用controls:TreeViewItem.Header,不能直接加在controls:TreeViewItem标签下,报错,可能是sl toolkit的问题,因为我运行他的demo直接在controls:TreeViewItem下加控件也报错
    Donnerstag, 13. November 2008 15:36
    Moderator

Alle Antworten

  •  

    你是用Silverlight Toolkit 的三方控件包里面的TreeView嘛?
    Donnerstag, 13. November 2008 03:10
    Moderator
  •  

    是呀 就是用的Silverlight Toolkit 上的TreeView。

    Donnerstag, 13. November 2008 03:12
  • Code Snippet

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Windows.Controls;

    namespace SilverlightApplication2
    {
        public partial class Page : UserControl
        {
            public Page()
            {
                InitializeComponent();
            }

            private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
            {
                TreeViewItem item = new TreeViewItem();
                item = (TreeViewItem)mytree.SelectedItem;
                ItemCollection items = item.Items;
                tbx.Text = item.Header.ToString();
                cbx.Items.Clear();
                for (int i = 0; i < items.Count; i++ )
                {
                    cbx.Items.Add(((TreeViewItem)items[i]).Header.ToString());
                }
            }
        }
    }

     

     

    我这里没问题呀

    Donnerstag, 13. November 2008 03:45
    Moderator
  • 还是不行,总是在树里选择了在combobox里不显示。最后只好用textbox+treeview自定义了一个控件.

     

    Donnerstag, 13. November 2008 08:01
  • 喔,你是要在TreeViewItem 里面加 ComboBox是嘛?才看明白,不是点TreeviewItem,在外部的ComboBox显示TreeviewItem列表?

    Donnerstag, 13. November 2008 08:08
    Moderator
  • 是呀,哈哈!我没有说清楚!不好意思。
    Donnerstag, 13. November 2008 08:41
  • Code Snippet

    <UserControl x:Class="TreeviewAndCombobox.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <controls:TreeView Margin="5">
                <controls:TreeViewItem>
                    <controls:TreeViewItem.Header>
                        <Button Content="Button" />
                    </controls:TreeViewItem.Header>
                </controls:TreeViewItem>
                <controls:TreeViewItem>
                    <controls:TreeViewItem.Header>
                        <CheckBox Content="CheckBox" />
                    </controls:TreeViewItem.Header>
                </controls:TreeViewItem>
                <controls:TreeViewItem>
                    <controls:TreeViewItem.Header>
                        <TextBox Text="TextBox" />
                    </controls:TreeViewItem.Header>
                </controls:TreeViewItem>
                <controls:TreeViewItem>
                    <controls:TreeViewItem.Header>
                        <ComboBox x:Name="cbx" SelectionChanged="ComboBox_SelectionChanged">
                            <ComboBox.Items>
                                <TextBlock Text="111"/>
                                <TextBlock Text="222"/>
                                <TextBlock Text="333"/>
                            </ComboBox.Items>
                        </ComboBox>
                    </controls:TreeViewItem.Header>
                </controls:TreeViewItem>
            </controls:TreeView>
        </Grid>
    </UserControl>

     

     

    今天测了,可以啊,不过要用controls:TreeViewItem.Header,不能直接加在controls:TreeViewItem标签下,报错,可能是sl toolkit的问题,因为我运行他的demo直接在controls:TreeViewItem下加控件也报错
    Donnerstag, 13. November 2008 15:36
    Moderator