ControlExtession { #region BindCommand 绑定命令和命令事件到宿主UI BindCommand(this UIElement @ui, ICommand com, Action<object, ExecutedRoutedEventArgs> call) { var bind = new CommandBinding(com); bind.Executed += new ExecutedRoutedEventHandler(call); @ui.CommandBindings.Add(bind); } 绑定RelayCommand命令到宿主UI BindCommand(this UIElement @ui, RelayCommand<object> com) { var bind = new CommandBinding(com); bind.Executed += delegate(object sender, ExecutedRoutedEventArgs e) { com.ExecuteCommand(e.Parameter); }; @ui.CommandBindings.Add(bind); } #endregion #region TreeView操作扩展方法 Returns the TreeViewItem of a data bound object. TreeViewItem GetItemFromObject(this TreeView treeView, object obj) { try { DependencyObject dObject = GetContainerFormObject(treeView, obj); TreeViewItem tvi = dObject as TreeViewItem; while (tvi == null) { dObject = VisualTreeHelper.GetParent(dObject); tvi = dObject as TreeViewItem; } return tvi; } catch { } return null; } private static DependencyObject GetContainerFormObject(ItemsControl item, object obj) { if (item == null) return null; DependencyObject dObject = null; dObject = item.ItemContainerGenerator.ContainerFromItem(obj); if (dObject != null) return dObject; var query = from childItem in item.Items.Cast<object>() let childControl = item.ItemContainerGenerator.ContainerFromItem(childItem) as ItemsControl select GetContainerFormObject(childControl, obj); return query.FirstOrDefault(i => i != null); } Selects a data bound object of a TreeView. SelectObject(this TreeView treeView, object obj) { treeView.SelectObject(obj, true); } Selects or deselects a data bound object of a TreeView. SelectObject(this TreeView treeView, object obj, bool selected) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { tvi.IsSelected = selected; } } Returns if a data bound object of a TreeView is selected. IsObjectSelected(this TreeView treeView, object obj) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { return tvi.IsSelected; } return false; } Returns if a data bound object of a TreeView is focused. IsObjectFocused(this TreeView treeView, object obj) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { return tvi.IsFocused; } return false; } Expands a data bound object of a TreeView. ExpandObject(this TreeView treeView, object obj) { treeView.ExpandObject(obj, true); } Expands or collapses a data bound object of a TreeView. ExpandObject(this TreeView treeView, object obj, bool expanded) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { tvi.IsExpanded = expanded; if (expanded) { // update layout, so that following calls to f.e. SelectObject on child nodes will // find theire TreeViewNodes treeView.UpdateLayout(); } } } Returns if a douta bound object of a TreeView is expanded. IsObjectExpanded(this TreeView treeView, object obj) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { return tvi.IsExpanded; } return false; } Retuns the parent TreeViewItem. TreeViewItem GetParentItem(this TreeViewItem item) { var dObject = VisualTreeHelper.GetParent(item); TreeViewItem tvi = dObject as TreeViewItem; while (tvi == null) { dObject = VisualTreeHelper.GetParent(dObject); tvi = dObject as TreeViewItem; } return tvi; } #endregion }
View Code2.3配色资源Colors.xaml