如何获取所选TreeViewItem的文本值?

时间:2018-12-17 12:14:14

标签: c# wpf events

我从数据库生成 Section Map. Variables (T1 T2 : Type) (f : nat -> T1 -> T2). Definition indexed_map (s : list T1) := let fix imap s index : list T2 := match s with | [] => [] | hd :: tl => (f index hd) :: imap tl (index + 1) end in imap s 0. End Map. Arguments indexed_map [T1 T2]. Program Fixpoint failing (ft : FType) {measure (ftype_size ft)} : FType := match ft with | Base _ => ft | Cons hd tl => match hd with | NestedList l rs => Base (NestedList l (indexed_map (fun i r' => (failing r')) rs)) | _ => ft end end. Admit Obligations. 的代码正在运行,但是现在我想基于所选的TreeView打开新窗口,但是我不确定如何访问{{ 1}}事件处理程序。

Compiler error in event handler

TreeViewItem Xaml:

SelectedItemChanged

背后的代码:

TreeView

2 个答案:

答案 0 :(得分:0)

您需要使用事件中的属性来访问所选的TreeViewItem(在您的情况下为DataRowView)。我对SelectedItemChanged中的示例进行了一些修改,以使其与您的代码匹配。注意事件数据如何包含所选的DataRowView。您需要使用事件数据来获取选定的项目,然后您可以对项目进行任何操作。

private void Tree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<Object> e)
{
    var item = (DataRowView) e.NewValue;
    System.Windows.MessageBox.Show(item["Id"].ToString());
}

答案 1 :(得分:0)

您可以使用WPF Behaviors

创建可绑定的SelectedItem属性。
public class perTreeViewHelper : Behavior<TreeView>
{
    public object BoundSelectedItem
    {
        get => GetValue(BoundSelectedItemProperty);
        set => SetValue(BoundSelectedItemProperty, value);
    }

    public static readonly DependencyProperty BoundSelectedItemProperty =
        DependencyProperty.Register("BoundSelectedItem",
            typeof(object),
            typeof(perTreeViewHelper),
            new FrameworkPropertyMetadata(null,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                OnBoundSelectedItemChanged));

    private static void OnBoundSelectedItemChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        if (args.NewValue is perTreeViewItemViewModelBase item)
            item.IsSelected = true;
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.SelectedItemChanged += OnTreeViewSelectedItemChanged;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.SelectedItemChanged -= OnTreeViewSelectedItemChanged;
        base.OnDetaching();
    }

    private void OnTreeViewSelectedItemChanged(object obj, RoutedPropertyChangedEventArgs<object> args)
    {
        BoundSelectedItem = args.NewValue;
    }
}

我的blog post上的WPF中有更多详细信息和更广泛的一般性讨论TreeViews。