このようなデータソースにバインドする TreeView があります
<TreeView ItemsSource="{Binding Data}" Width="190" >
JinkData は、ViewModel でクラスのプロパティとして定義されています。プロパティは次のように定義されます
public Collection<JinkData> Data { get; set; }
TreeView のどのノードが現在選択されているかを示すプロパティ IsSelected があり、次のコードを使用して、「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));
}
私が直面している問題は、 IsSelected の Set の「この」ポインターがコレクション JinkData であることですが、このポインターをコレクション全体の代わりに selectedJinkData にしたかったのです。現在選択されている JinkData を TreeView から取得するにはどうすればよいですか?
どうすればいいですか?