WPF TreeView 模糊查询节点
<TextBox x:Name="tbSeach" Height="25" Width="60" HorizontalAlignment="Center" VerticalAlignment="Top" TextChanged="tbSeach_TextChanged"/>
private void tbSeach_TextChanged(object sender, TextChangedEventArgs e)
{
_seachfooList = new List<FooViewModel>();
foreach (FooViewModel model in treeGoods.Items)
{
foreach (FooViewModel foo in findNode(model, tbSeach.Text.Trim()))
{
foo.IsChecked = true;
};
}
}
//模糊查询算法
private List<FooViewModel> findNode(FooViewModel tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Name.IndexOf(strValue) > -1 && !string.IsNullOrEmpty(strValue))
{
_seachfooList.Add(tnParent);
}
foreach (FooViewModel foo in tnParent.Children)
{
foo.IsChecked = false;
findNode(foo, strValue);
}
return _seachfooList;
}