从根容器获取 TreeViewItem 数据上下文

发布于 2024-11-03 00:13:23 字数 423 浏览 0 评论 0原文

我有一个带有两个使用控件的窗口。一个封装了 TreeView 控件,另一个只是所选 TreeViewItems 详细信息的字符串表示形式。

TreeView 控件已经是自给自足的 - 它将用内容项填充自身。每个 TreeViewItem 都有一个数据上下文。

这是我需要发生的事情:

  1. 每当用户单击某个项目时,它都会生成一个 Selected 路由事件,该事件在我的窗口的根容器中被捕获。
  2. 我需要事件处理程序来处理 Selected 路由事件,以便能够从所选 TreeViewItem 的数据上下文中获取一些数据(属性值)。
  3. 根据该值,我的事件处理程序将创建一个DetailsView 控件并用数据填充它。

在处理选定事件方面,我的方法正确吗?如何从所选项目数据上下文中获取属性值?

谢谢。

I have a Window with two use controls. One encapsulates a TreeView control and another is marely a string representation of a selected TreeViewItems details.

TreeView controls is already self sufficient - it will populate itself with content items. Each TreeViewItem has a datacontext.

Here is what I need to happen:

  1. Whenever a user clicks an item, it generates a Selected routed event that is being caught at the root container of my window.
  2. I need the event handler, that handles Selected routed event to be able to get some data (a property value) from the selected TreeViewItem's data context.
  3. Based on this value, my event handler will create a DetailsView control and will populate it with data.

Is my approach correct, in respect to handling the Selected events? How can I get the property value from my selected items data context?

Thank you.

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

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

发布评论

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

评论(1

烟沫凡尘 2024-11-10 00:13:23

将属性绑定到 TreeView 的 SelectedItem 可以解决您的问题。下面的代码表达了我正在考虑的内容。对于您拥有的两个 UC,您可以公开 SelectedItem 的一个属性。

XAML:

<TreeView x:Name="Tree1">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>
<TextBlock Text="{Binding ElementName=Tree1,Path=SelectedItem.Name}" />

的实体

public class Item
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string name;
    private Collection<Item> items;

    public Collection<Item> Items { get { return items; } }

    public Item()
    {
        items = new Collection<Item>();
    }

    public string Name
    {
        get { return name; }
        set { name = value; OnPropertyChanged("Name"); }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

项目绑定源

Item item = new Item { Name = "A" };
item.Items.Add(new Item { Name = "A_1" });
item.Items.Add(new Item { Name = "A_2" });
item.Items.Add(new Item { Name = "A_3" });
Tree1.ItemsSource = new Collection<Item>() { item };

Binding a property to the SelectedItem of TreeView can fix your issue. The code below express what I'm considering. For the two UC you have, you can expose one property for the SelectedItem.

XAML:

<TreeView x:Name="Tree1">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>
<TextBlock Text="{Binding ElementName=Tree1,Path=SelectedItem.Name}" />

Entity Of Item

public class Item
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string name;
    private Collection<Item> items;

    public Collection<Item> Items { get { return items; } }

    public Item()
    {
        items = new Collection<Item>();
    }

    public string Name
    {
        get { return name; }
        set { name = value; OnPropertyChanged("Name"); }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Binding Source

Item item = new Item { Name = "A" };
item.Items.Add(new Item { Name = "A_1" });
item.Items.Add(new Item { Name = "A_2" });
item.Items.Add(new Item { Name = "A_3" });
Tree1.ItemsSource = new Collection<Item>() { item };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文