Silverlight 树形控件用法(数据绑定、右键菜单)

1、树形控件定义如下:

        <controls:TreeView x:Name="MainTree" ItemsSource="{Binding ItemsRoot}" MouseRightButtonDown="MainTree_MouseRightButtonDown" >
            <controls:TreeView.ItemTemplate>
                <common:HierarchicalDataTemplate ItemsSource="{Binding TemplateItems}">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="15" Height="15" Source="{Binding ImageUri}"/>
                        <HyperlinkButton x:Name="TemplateLinkButton" Content="{Binding DisplayName}" Click="TemplateLinkButton_Clicked"/>
                        <TextBox Text="{Binding DisplayName, Mode=TwoWay}" Visibility="Collapsed" LostFocus="TextBox_LostFocus"/>
                    </StackPanel>
                </common:HierarchicalDataTemplate>
            </controls:TreeView.ItemTemplate>
        </controls:TreeView>

2、数据绑定:通过ItemsSource进行数据绑定,绑定到ObservableCollection,在ViewModel层中实现如下:

public class TemplateTreeViewModel : INotifyPropertyChanged
    {
        private static TemplateTreeViewModel instance = new TemplateTreeViewModel();
        private TemplateItem rootItem = new TemplateItem();
        private ObservableCollection<TemplateItem> itemsRoot = new ObservableCollection<TemplateItem>();

        public static TemplateTreeViewModel Instance { get { return instance; } }

        public ObservableCollection<TemplateItem> ItemsRoot
        {
            get { return itemsRoot; }
            set
            {
                itemsRoot = value;
                NotifyPropertyChanged("ItemsRoot");
            }
        }

        private TemplateTreeViewModel()
        {
            rootItem = new TemplateItem() { DisplayName = "模板列表", IsTemplate = false };
            ItemsRoot.Add(rootItem);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

3、右键菜单的定义:通过MouseRightButtonClick来定义:
namespace Controls
{
    /// <summary>
    /// Interaction logic for TemplateTreeView.xaml
    /// </summary>
    public partial class TemplateTreeView : UserControl
    {
        private ContextMenu folderMenu = new ContextMenu();
        private ContextMenu templateMenu = new ContextMenu();
        private TextBlock currentTextBlock = null;
        private TextBox currentTextBox = null;
        private TemplateItem currentContext = null;

        public event EventHandler<TemplateSelectedEventArgs> TemplateSelected = null;

        public TemplateTreeView()
        {
            InitializeComponent();

            this.DataContext = TemplateTreeViewModel.Instance;

            MenuItem addFolderItem = new MenuItem();
            addFolderItem.Header = "创建文件夹";
            addFolderItem.Foreground = new SolidColorBrush(Colors.Black);
            addFolderItem.Click += new RoutedEventHandler(addFolderItem_Click);
            MenuItem addTemplateItem = new MenuItem();
            addTemplateItem.Header = "添加模板";
            addTemplateItem.Foreground = new SolidColorBrush(Colors.Black);
            addTemplateItem.Click += new RoutedEventHandler(addTemplateItem_Click);
            MenuItem renameItem = new MenuItem();
            renameItem.Header = "重命名";
            renameItem.Foreground = new SolidColorBrush(Colors.Black);
            renameItem.Click += new RoutedEventHandler(renameItem_Click);
            folderMenu.Items.Add(addFolderItem);
            folderMenu.Items.Add(addTemplateItem);
            folderMenu.Items.Add(renameItem);

            MenuItem renameItem2 = new MenuItem();
            renameItem2.Header = "重命名";
            renameItem2.Foreground = new SolidColorBrush(Colors.Black);
            renameItem2.Click += new RoutedEventHandler(renameItem_Click);
            templateMenu.Items.Add(renameItem2);
        }

        void renameItem_Click(object sender, RoutedEventArgs e)
        {
            if (currentTextBlock != null)
            {
                TextBox textBox = (currentTextBlock.Parent as StackPanel).Children[2] as TextBox;
                currentTextBox = textBox;
                currentTextBlock.Visibility = System.Windows.Visibility.Collapsed;
                textBox.Visibility = System.Windows.Visibility.Visible;
                textBox.Focus();
                textBox.SelectAll();
            }
        }

        void addTemplateItem_Click(object sender, RoutedEventArgs e)
        {
            if (currentContext != null)
            {
                OpenTemplateWindow openTemplateWindow = openTemplateWindow = new OpenTemplateWindow();
#if !SILVERLIGHT
                openTemplateWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                var dialogResult = openTemplateWindow.ShowDialog();
                if (dialogResult ?? false)
                {
                    currentContext.TemplateItems.Add(new TemplateItem()
                    {
                        DisplayName = openTemplateWindow.TemplateName,
                        TemplateName = openTemplateWindow.TemplateName,
                        IsTemplate = true
                    });
                }
#else
                openTemplateWindow.Closed += (sender2, e2) =>
                {
                    currentContext.TemplateItems.Add(new TemplateItem()
                        {
                            DisplayName = openTemplateWindow.TemplateName,
                            TemplateName = openTemplateWindow.TemplateName,
                            IsTemplate = true
                        });
                };
                openTemplateWindow.Show();
#endif
            }
        }

        void addFolderItem_Click(object sender, RoutedEventArgs e)
        {
            if (currentContext != null)
            {
                currentContext.TemplateItems.Add(new TemplateItem() { DisplayName = "我的模板", IsTemplate = false });
            }
        }

        public TemplateTreeViewModel ViewModel
        {
            get { return this.DataContext as TemplateTreeViewModel; }
        }

        private void MainTree_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var textBlock = e.OriginalSource as TextBlock;
            if (textBlock != null)
            {
                currentTextBlock = textBlock;
                var item = textBlock.DataContext as TemplateItem;
                if (item != null)
                {
                    currentContext = item;
                    if (!item.IsTemplate)
                        folderMenu.IsOpen = true;
                    else
                        templateMenu.IsOpen = true;
                }
            }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            currentTextBlock.Visibility = System.Windows.Visibility.Visible;
            currentTextBox.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void MainTree_MouseDoubleClick(object sender, RoutedEventArgs e)
        {
            var textBlock = e.OriginalSource as TextBlock;
            if (textBlock != null)
            {
                currentTextBlock = textBlock;
                var item = textBlock.DataContext as TemplateItem;
                if (item != null)
                {
                    currentContext = item;
                    if (item.IsTemplate)
                    {
                        if (TemplateSelected != null)
                            TemplateSelected(this, new TemplateSelectedEventArgs() { TemplateName = item.TemplateName });
                    }
                }
            }
        }

#if SILVERLIGHT
                private void TemplateLinkButton_Clicked(object sender, RoutedEventArgs e)
                {
                    var hyperlink = sender as HyperlinkButton;
                    if (hyperlink != null)
                    {
                        var item = hyperlink.DataContext as TemplateItem;
                        if (item != null)
                        {
                            currentContext = item;
                            if (item.IsTemplate)
                            {
                                if (TemplateSelected != null)
                                    TemplateSelected(this, new TemplateSelectedEventArgs() { TemplateName = item.TemplateName });
                            }
                        }
                    }
                }
#endif
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值