如何使用MVVM扩展Silverlight工具包树视图中的一个节点?

发布于 2024-12-04 17:27:24 字数 1630 浏览 5 评论 0原文

如何使用 MVVM 展开树视图中的节点?

我有斧头级树视图,我创建了自己的类将其绑定到 TreeView 控件。

当我创建 TreeView 列表时,我确实设法将 IsSelected 属性设置为 true 。 所以我只需将我的 IsSelected 值绑定到 TreeViewItem IsSelected 属性,但它根本没那么简单......

这是我的类:

public class HierarchicalItem : Model
{
    public string Name { get; set; }
    public int Id { get; set; }

    private bool _IsSelected;
    public bool IsSelected
    {
        get { return _IsSelected; }
        set
        {
            if (_IsSelected != value)
            {
                _IsSelected = value;
                RaisePropertyChanged("IsSelected");
            }
        }
    }

    private ObservableCollection<HierarchicalItem> _children;
    public ObservableCollection<HierarchicalItem> Children
    {
        get
        {
            return _children;
        }
        set
        {
            if (_children != value)
            {
                _children = value;
                RaisePropertyChanged("Children");
            }
        }
    }
}

我尝试使用 IsSelectedIsExpanded

<controls:TreeView.ItemContainerStyle>
    <Style TargetType="controls:TreeViewItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}" />
    </Style>
</controls:TreeView.ItemContainerStyle>

但它返回, IsSelectedIsExpanded 是只读的(这在 WPF 中工作)。

我已经为此寻找了很多解决方案,但没有一个对我有用,因为我在列表框中有动态树视图,并且在每个树视图中我必须将整个路径扩展到具有给定 ID 的节点。例如,我想展开每个树视图中 Id = 30 的所有节点。

How can I expand a node in a treeview using MVVM?

I have a x level treeview and I made my own class to bind it to the TreeView control.

I did manage to set IsSelected property to true when I create the TreeView list.
So I just have to bind my IsSelected value to the TreeViewItem IsSelected property, but it's not that simple at all...

Here is my class:

public class HierarchicalItem : Model
{
    public string Name { get; set; }
    public int Id { get; set; }

    private bool _IsSelected;
    public bool IsSelected
    {
        get { return _IsSelected; }
        set
        {
            if (_IsSelected != value)
            {
                _IsSelected = value;
                RaisePropertyChanged("IsSelected");
            }
        }
    }

    private ObservableCollection<HierarchicalItem> _children;
    public ObservableCollection<HierarchicalItem> Children
    {
        get
        {
            return _children;
        }
        set
        {
            if (_children != value)
            {
                _children = value;
                RaisePropertyChanged("Children");
            }
        }
    }
}

I tried that with IsSelected and IsExpanded:

<controls:TreeView.ItemContainerStyle>
    <Style TargetType="controls:TreeViewItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}" />
    </Style>
</controls:TreeView.ItemContainerStyle>

But it returns, that IsSelected and IsExpanded is read only (this works in WPF).

I have looked many solutions for this, but none works for me, because I have dynamic treeviews in a listbox and in each of the treeviews I have to expand the whole path to the node that has a given Id. For example I want to expand all nodes that have Id = 30 in each treeview.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2024-12-11 17:27:24

不幸的是,在 Silverlight 4 中不可能在样式设置器上设置绑定。

设置 IsExpanded 的一种方法(诚然不是那么漂亮)是在 TreeViewItem 的控件模板中的 ToggleButton 上设置绑定。如果您转到 http://msdn.microsoft。 com/en-us/library/dd728671(v=vs.95).aspx 可以获得TreeViewItem的默认控件模板。您可以复制此内容并替换以下内容:

Unfortunately, it is not possible to set bindings on style setters in Silverlight 4.

One way to set IsExpanded, which admittedly isn't all that pretty, is to set the binding on the ToggleButton in the TreeViewItem's control template. If you go to http://msdn.microsoft.com/en-us/library/dd728671(v=vs.95).aspx you can get the default control template for TreeViewItem. You could copy this and replace the following:

with

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文