silverlight Treeview 节点如何默认展开
如题
[解决办法]
如果是使用 TreeViewItem 填充的 TreeView ,那 TreeViewItem 本身就有 IsExpanded 属性。
如果是使用绑定,并使用分层模板,那么只能引用
System.Windows.Controls.Toolkit.dll ,里面有一个 TreeViewExtensions,摘取部分方法定义:
public static class TreeViewExtensions{ // // 摘要: // Collapse all of the items in a TreeView. // // 参数: // view: // The TreeView. // // 异常: // System.ArgumentNullException: // view is null. public static void CollapseAll(this TreeView view); // // 摘要: // Expand all of the items in a TreeView. // // 参数: // view: // The TreeView. // // 异常: // System.ArgumentNullException: // view is null. public static void ExpandAll(this TreeView view); // // 摘要: // Expand a path from the TreeViewItem to the root of the TreeView. // // 参数: // item: // The TreeViewItem. // // 异常: // System.ArgumentNullException: // item is null. public static void ExpandPath(this TreeViewItem item); // // 摘要: // Expand the given path of items starting from the TreeView's root. // // 参数: // view: // The TreeView. // // items: // The sequence of items corresponding to the path to expand. // // 类型参数: // T: // The type of items provided in items. // // 异常: // System.ArgumentNullException: // view is null. // // System.ArgumentNullException: // items is null. public static void ExpandPath<T>(this TreeView view, IEnumerable<T> items); // // 摘要: // Expand the given path of items starting from the TreeView's root. // // 参数: // view: // The TreeView. // // items: // The sequence of items corresponding to the path to expand. // // 异常: // System.ArgumentNullException: // view is null. // // System.ArgumentNullException: // items is null. public static void ExpandPath(this TreeView view, params object[] items); // // 摘要: // Expand the given path of items starting from the TreeView's root. // // 参数: // view: // The TreeView. // // comparisonSelector: // A function that takes a TreeViewItem's item and returns a value to compare // against elements of the given items. The item itself will be used if comparisonSelector // is null. // // items: // The sequence of items corresponding to the path to expand. // // 类型参数: // T: // The type of items provided in items. // // 异常: // System.ArgumentNullException: // view is null. // // System.ArgumentNullException: // items is null. public static void ExpandPath<T>(this TreeView view, Func<object, T> comparisonSelector, IEnumerable<T> items); // // 摘要: // Expand the given path of items starting from the TreeView's root. // // 参数: // view: // The TreeView. // // comparisonSelector: // A function that takes a TreeViewItem's item and returns a value to compare // against elements of the given items. The item itself will be used if comparisonSelector // is null. // // items: // The sequence of items corresponding to the path to expand. // // 类型参数: // T: // The type of items provided in items. // // 异常: // System.ArgumentNullException: // view is null. // // System.ArgumentNullException: // items is null. public static void ExpandPath<T>(this TreeView view, Func<object, T> comparisonSelector, params T[] items); // // 摘要: // Expand the path from the SelectedItem to the root of the TreeView. // // 参数: // view: // The TreeView. // // 异常: // System.ArgumentNullException: // view is null. public static void ExpandSelectedPath(this TreeView view); // // 摘要: // Expand a specified number of layers in a TreeView. // // 参数: // view: // The TreeView. // // depth: // The number of layers to expand. // // 异常: // System.ArgumentNullException: // view is null. public static void ExpandToDepth(this TreeView view, int depth); // // 摘要: // Get the TreeViewItem already created that is used to represent the given // item. // // 参数: // view: // The TreeView. // // item: // The item being represented. // // 返回结果: // The TreeViewItems that represents the given item, or null if no container // was found. // // 异常: // System.ArgumentNullException: // view is null. // // 备注: // If multiple TreeViewItems represent the same item, the first item found via // a breadth-first search will be used. public static TreeViewItem GetContainerFromItem(this TreeView view, object item); // // 摘要: // Get the TreeViewItem containers of a TreeView. // // 参数: // view: // The TreeView. // // 返回结果: // The TreeViewItem containers of a TreeView. // // 异常: // System.ArgumentNullException: // view is null. public static IEnumerable<TreeViewItem> GetContainers(this TreeView view); // // 摘要: // Get the child TreeViewItem containers of a TreeViewItem. // // 参数: // item: // The TreeViewItem. // // 返回结果: // The child TreeViewItem containers of a TreeViewItem. // // 异常: // System.ArgumentNullException: // item is null. public static IEnumerable<TreeViewItem> GetContainers(this TreeViewItem item); // // 摘要: // Get the TreeViewItems already created that are used to represent the given // item. // // 参数: // view: // The TreeView. // // item: // The item being represented. // // 返回结果: // A sequence of TreeViewItems that represent the given item, or an empty sequence // if none were found. // // 异常: // System.ArgumentNullException: // view is null. public static IEnumerable<TreeViewItem> GetContainersFromItem(this TreeView view, object item); // // 摘要: // Gets the depth of a TreeViewItem in its TreeView (using a zero-based index). // // 参数: // item: // The TreeViewItem. // // 返回结果: // The depth of a TreeViewItem in its TreeView (using a zero-based index). // // 异常: // System.ArgumentNullException: // item is null. // // System.ArgumentException: // item is not in a TreeView. public static int GetDepth(this TreeViewItem item); // // 摘要: // Get the TreeViewItem containers of a TreeView. // // 参数: // view: // The TreeView. // // 返回结果: // The TreeViewItem containers of a TreeView. // // 异常: // System.ArgumentNullException: // view is null. public static IEnumerable<TreeViewItem> GetDescendantContainers(this TreeView view); // // 摘要: // Get the descendant TreeViewItem containers of a TreeViewItem. // // 参数: // item: // The TreeViewItem. // // 返回结果: // The descendant TreeViewItem containers of a TreeViewItem. // // 异常: // System.ArgumentNullException: // item is null. public static IEnumerable<TreeViewItem> GetDescendantContainers(this TreeViewItem item);}
[解决办法]
在 LayoutUpdated 事件中可以展开全树
this.treeView1.LayoutUpdated += (sender, e) =>{ this.treeView1.ExpandAll();};
[解决办法]
获取TreeViewItem,然后设置TreeViewItem.IsExpanded=True就会自动默认展开树型所有选项。
[解决办法]
界面呈现和后台的model应该绑定,如果要将所有节点展开,在每一项对应的model中加个bool类型的属性,比如IsExpanded,然后前台加个style
<Style x:Key="treeViewStyle" TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}" /></Style>
[解决办法]
额 IsExpanded 不可以吗
[解决办法]