我有一个TreeView,它绑定到像这样的数据源
<TreeView ItemsSource="{Binding Data}" Width="190" >
JinkData在我的ViewModel中定义为类的属性。该属性定义如下
public Collection<JinkData> Data { get; set; }
我有一个属性IsSelected,告诉我当前选择了TreeView的哪个节点,然后我可以使用'this'指针使用以下代码获取所选节点。
private static object _selectedItem = null;
// This is public get-only here but you could implement a public setter which also selects the item.
// Also this should be moved to an instance property on a VM for the whole tree, otherwise there will be conflicts for more than one tree.
public static object SelectedItem
{
get { return _selectedItem; }
private set
{
if (_selectedItem != value)
{
_selectedItem = value;
OnSelectedItemChanged();
}
}
}
static virtual void onselecteditemchanged()
{
// raise event / do other things
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (_isSelected != value)
{
_isSelected = value;
OnPropertyChanged("IsSelected");
if (_isSelected)
{
SelectedItem = this;
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
我面临的问题是Set of IsSelected中的'this'指针是集合JinkData,但我希望这个指针成为整个集合的selectedJinkData insead。如何从TreeView获取当前选定的JinkData?
我该怎么做?