WPF Treeview 仅展开第一个节点和所选项目?
我在 WPF 中使用树视图,并且我不想在重新加载绑定集合时丢失状态(展开和焦点)。
在第一次加载时,很容易仅扩展第一个节点,我使用以下代码:
private void ExpandFirstNodeTree()
{
foreach (var item in TreeviewModel.Items)
{
TreeViewItem itm = (TreeViewItem)TreeviewModel.ItemContainerGenerator.ContainerFromItem(item);
if (itm == null) continue;
itm.IsExpanded = true;
}
}
我使用 DependencyProprety 来选择一个项目。我探索 TreeView,找到 TreeViewItem 并将项目“IsSelected”属性设置为 true。
private static readonly DependencyProperty SelectedEntityCodeProperty =
DependencyProperty.Register(PropertyHelper.GetName((EntitiesTreeview e) => e.SelectedEntityCode), typeof (string), typeof (EntitiesTreeview));
public string SelectedEntityCode
{
get { return (string) GetValue(SelectedEntityCodeProperty); }
set { SetValue(SelectedEntityCodeProperty, value); }
}
public EntitiesTreeview()
{
InitializeComponent();
Loaded += new RoutedEventHandler(EntitiesTreeview_Loaded);
}
private void LoadSelectedItem()
{
if ((!string.IsNullOrEmpty(SelectedEntityCode))
&& (TreeviewEntity.SelectedItem == null))
ChangeSelectedItem<ENTITY>(SelectedEntityCode, TreeviewEntity);
}
private bool ChangeSelectedItem<T>(string entityCode, ItemsControl itemsControl) where T : ENTITYBASE
{
if (itemsControl != null)
{
foreach (var item in itemsControl.Items)
{
var currentContainer = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
if ((currentContainer != null)
&& (item is T)
&& ((item as T).CCODE == entityCode))
{
currentContainer.IsSelected = true;
var selectMethod = typeof (TreeViewItem).GetMethod("Select", BindingFlags.NonPublic | BindingFlags.Instance);
selectMethod.Invoke(currentContainer, new object[] {true});
return true;
}
if (ChangeSelectedItem<T>(entityCode, currentContainer))
return true;
}
}
return false;
}
我的问题是,当我重新加载项目集合时,焦点会丢失(所选项目)并且展开的项目会折叠。如何分离绑定项目和 ui ? 最后一点,我想以编程方式设置选定的项目。当依赖属性更改时,如何重新加载所选项目?
我已经看过 josh smith 解决方案( http://www.codeproject .com/KB/WPF/TreeViewWithViewModel.aspx ),但我不想使用 ViewModel 集合进行绑定。我有不同的对象类型来绑定和使用 ViewModel 将是痛苦的... IMO :)
I use a treeview in WPF and I don't want to lose the state (expanding and focus) when I reload the binded collection.
At the first load it's easy to expand just the first node, i use the following code :
private void ExpandFirstNodeTree()
{
foreach (var item in TreeviewModel.Items)
{
TreeViewItem itm = (TreeViewItem)TreeviewModel.ItemContainerGenerator.ContainerFromItem(item);
if (itm == null) continue;
itm.IsExpanded = true;
}
}
I use a DependencyProprety to select an item. I explore the TreeView, find the TreeViewItem and set the item "IsSelected" property to true.
private static readonly DependencyProperty SelectedEntityCodeProperty =
DependencyProperty.Register(PropertyHelper.GetName((EntitiesTreeview e) => e.SelectedEntityCode), typeof (string), typeof (EntitiesTreeview));
public string SelectedEntityCode
{
get { return (string) GetValue(SelectedEntityCodeProperty); }
set { SetValue(SelectedEntityCodeProperty, value); }
}
public EntitiesTreeview()
{
InitializeComponent();
Loaded += new RoutedEventHandler(EntitiesTreeview_Loaded);
}
private void LoadSelectedItem()
{
if ((!string.IsNullOrEmpty(SelectedEntityCode))
&& (TreeviewEntity.SelectedItem == null))
ChangeSelectedItem<ENTITY>(SelectedEntityCode, TreeviewEntity);
}
private bool ChangeSelectedItem<T>(string entityCode, ItemsControl itemsControl) where T : ENTITYBASE
{
if (itemsControl != null)
{
foreach (var item in itemsControl.Items)
{
var currentContainer = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
if ((currentContainer != null)
&& (item is T)
&& ((item as T).CCODE == entityCode))
{
currentContainer.IsSelected = true;
var selectMethod = typeof (TreeViewItem).GetMethod("Select", BindingFlags.NonPublic | BindingFlags.Instance);
selectMethod.Invoke(currentContainer, new object[] {true});
return true;
}
if (ChangeSelectedItem<T>(entityCode, currentContainer))
return true;
}
}
return false;
}
My problem is, when I reload the items collection, the focus is lost (selected item) and expanded items are collapsed. How can I separate bound items and ui ?
Last point, I want to set a selected item programmatically. How can I reload the selected item when the dependency property changed ?
I've already had a look to the josh smith solution ( http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx ) but I don't want to use a ViewModel collection for my binding. I have different object type to bind and use ViewModel will be to painfull... IMO :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,您现在遇到的问题确切地说明了为什么不使用视图模型太痛苦了。
如果构建视图模型,则可以在视图模型类中实现
IsSelected
和IsExpanded
属性。然后,您可以将 TreeViewItem 的相关属性绑定到它们。一旦执行此操作,UI 中状态的更改将反映在视图模型数据中,并且如果您从数据重新加载 UI,状态更改将被保留。这是实现您想要实现的目标的最简单的方法。作为一个额外的好处,您可以丢弃上面发布的难以测试的神秘代码隐藏的最后一部分。
In fact, the problem you're experiencing right now demonstrates exactly why not using a view model is too painful.
If you construct a view model, you can implement
IsSelected
andIsExpanded
properties in the view model class. You can then bind the relevant properties of theTreeViewItem
to them. Once you do this, changes to the state in the UI will be reflected in the view model data, and if you reload the UI from the data, the changes in state will be retained.This is the simplest possible way to achieve what you're trying to achieve. As an additional benefit, you can discard every last scrap of the mystifyingly hard-to-test code-behind you've posted above.