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 } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。