我希望使用c#在Treeview项目选择中打开属性网格

时间:2017-07-13 15:56:28

标签: c# treeview propertygrid

我一直在尝试在选择树节点时打开属性网格,然后将属性网格的值绑定到C#中的选定树节点。这样,如果我创建一个新的树节点,它在属性网格中有自己的值。

任何人都可以举例说明如何做或可能建议我使用下面的代码,

public partial class WPFpropertygriddemo : Window
{
    public WPFpropertygriddemo()
    {
        InitializeComponent();
        _propertyGrid.Visibility = Visibility.Hidden;
    }

    private void myTreeView_Selected(object sender, RoutedEventArgs e)
    {
        TreeViewItem tvi = e.OriginalSource as TreeViewItem;
        Item itemsub = (Item)tvi.Header;
        if (itemsub != null)
        {
            _propertyGrid.Visibility = Visibility.Visible;
            // System.Windows.MessageBox.Show(itemsub.DisplayValue);
            _propertyGrid.SelectedObject = itemsub;
        }
    }
}
public class Item : MyModelINotifyProperty
{
    public string DisplayValue { get; set; }

    private bool _isSelected = false;

    public bool IsSelected
    {
        get
        {
            return _isSelected;
        }
        set
        {
            _isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }

    private string _sample;

    public string Sample
    {
        get
        {
            return _sample;
        }
        set
        {
            _sample = value;
            OnPropertyChanged("Sample");
        }
    }
}

public class MyViewModel : MyModelINotifyProperty
{
    public ObservableCollection<Item> Items
    {
        get
        {
            return new ObservableCollection<Item>()
        {
            new Item() {DisplayValue = "Item1", IsSelected = false, Sample = "Sample: I am Item1"},
            new Item() {DisplayValue = "Item2", IsSelected = true, Sample = "Sample: I am Item2"},
            new Item() {DisplayValue = "Item3", IsSelected = false, Sample = "Sample: I am Item3"}
        };
        }
    }
}



public abstract class MyModelINotifyProperty : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

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

我不明白TreeViewItem的定义在哪里以及代码行是什么 “TreeViewItem tvi = e.OriginalSource as TreeViewItem; 在做什么?我在TreeViewItem和Original源代码中出错。我是否应该添加一个使用e.original的引用?

由于 SREE。

0 个答案:

没有答案