我想在我的树视图和数据网格之间创建关系我有一个名为 SelectedTestElement 的方法,它调用 SearchGrid 方法
视图模型:
public TestElementViewModel SelectedTestElement
{
set
{
if (_selectedTestElement == value) return;
this._ selectedTestElement = value;
SearchGrid(value.Id, new List< TestElementViewModel>( testElementViewModel.GetAllTreeNodes()));
}
get
{
return this._ selectedTestElement;
}
}
我想在我的 xaml TreeView 中绑定这个属性
<TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding MessageElements, Mode=TwoWay}" TreeViewItem.Selected="{Binding SelectedTestElement, Mode=TwoWay}" />
treeView.Selected 是在树视图中进行属性调用的最佳方式吗?
编辑:
视图模型:
我选择一个树节点,即 SelectedTreeNode 将绑定到 TreeView.SelectedItem
public TreeNodeViewModel SelectedTreeNode
{
set
{
if (_selectedTreeNode== value) return;
this._selectedTreeNode = value;
//search the treenode id
SearchGrid(value.Id, TestFieldVModel.GetAllTestField());
}
get
{
return this._selectedTreeNode;
}
}
//进行搜索并突出显示匹配的id网格
public void SearchGrid(int id, ObservableCollection<TestFieldViewModel> msgField)
{
foreach (var c in msgField)
{
c.DisplayColor = c.Id == id ? Brushes.DarkOrange : Brushes.Black;
c.DisplayFont = c.Id == id ? FontWeights.Bold : FontWeights.Normal;
// SearchGrid(id, c.GetAllTestField());
}
}
XAML:
<TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding TreeNodes, Mode=TwoWay}" TreeView.SelectedItem ="{Binding Path= SelectedTreeNode, Mode=TwoWay}" >