本文介绍了在wpf中移动选定的treeviewitem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题.在wpf中,我有一个由数据绑定填充的树形视图.以下代码是完整代码的子集.

XAML:

I have run into a problem. In wpf I have a treeview populated by databinding. The following code is a subset of the complete code.

XAML:

<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:local="clr-namespace:WpfApplication1"

        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:TreeViewItemViewModel}" ItemsSource="{Binding Path=Children}" >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Data}"/>
                <Button Content="Inside" Click="button2_Click" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <TreeView Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="treeView1" VerticalAlignment="Top" Width="186" />
        <Button Content="Outside" Height="23" HorizontalAlignment="Left" Margin="204,41,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
    </Grid>
</Window>




C#




C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        ObservableCollection<TreeViewItemViewModel> nodes;
        protected override void OnActivated(EventArgs e)
        {
            nodes = new ObservableCollection<TreeViewItemViewModel>();
            nodes.Add(new TreeViewItemViewModel("1"));
            nodes.Add(new TreeViewItemViewModel("2"));
            nodes.Add(new TreeViewItemViewModel("3"));
            TreeViewItemViewModel viewModel = new TreeViewItemViewModel("4");
            viewModel.Parent = nodes[0];
            nodes[0].Children.Add(viewModel);
            treeView1.ItemsSource = nodes;
            base.OnActivated(e);
        }
        
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            TreeViewItemViewModel viewModel = nodes[0].Children[0];
            nodes[0].Children.Remove(viewModel);
            viewModel.Parent = nodes[1];
            nodes[1].IsExpanded = true;
            nodes[1].Children.Add(viewModel);
            viewModel.IsExpanded = true;
            viewModel.IsSelected = true;
        }
    }
    public class TreeViewItemViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<TreeViewItemViewModel> Children { get; private set; }
        private bool _isExpanded;
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (_isExpanded != value)
                {
                    _isExpanded = value;
                    if (Parent != null)
                    {
                        Parent.IsExpanded = true;
                    }
                    OnPropertyChanged("IsExpanded");
                }
            }
        }
        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
        }
        public TreeViewItemViewModel Parent { get; set; }
        public TreeViewItemViewModel(string data)
        {
            Data = data;
            Children = new ObservableCollection<TreeViewItemViewModel>();
        }
        public string Data { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}





我正在尝试将treeviewitem"4"移动到treeviewitem"2"下.移动之后,我想选择移动的treeviewitem("4").如果我按下treeviewitem"3"的外部"或内部"按钮,则效果很好.但是,如果我按下treeviewitem"4"(我们正在移动的那个)的"inside"按钮,它将移动该项目,但不会选择它.我在做什么错?





I am trying to move treeviewitem "4" under treeviewitem "2". After the moving I want to select the moved treeviewitem ("4"). If I push the button "outside" or the button "inside" for treeviewitem "3" it works perfect. However, if I push the button "inside" for treeviewitem "4" (the one we are moving), it moves the item, but it does not select it. What am I doing wrong?

推荐答案


这篇关于在wpf中移动选定的treeviewitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 02:44