HTML5技术

WPF自定义控件与样式(15)-终结篇 - /*梦里花落知多少*/(4)

字号+ 作者:H5之家 来源:H5之家 2015-12-05 08:51 我要评论( )

ControlExtession{ #region BindCommand 绑定命令和命令事件到宿主UI BindCommand( this UIElement @ui, ICommand com, Action object , ExecutedRoutedEventArgs call){ var bind = new CommandBinding(com);bind.

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 Code

2.3配色资源Colors.xaml

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android 代码库(自定义一套 Dialog通用提示框 ) - 小小情意

    Android 代码库(自定义一套 Dialog通用提示框 ) - 小小情意

    2017-04-21 11:01

  • 自定义input默认placeholder样式 - 小碎步

    自定义input默认placeholder样式 - 小碎步

    2017-04-20 13:00

  • 移动端默认样式重置 - ^.GTR

    移动端默认样式重置 - ^.GTR

    2017-03-27 17:00

  • 从Visual Studio看微软20年技术变迁 - 葡萄城控件技术团队

    从Visual Studio看微软20年技术变迁 - 葡萄城控件技术团队

    2017-03-17 11:00

网友点评
c