我正在研究 WPF TreeView
,我想隐藏树的父级。对于这种情况,父母是:
var parent = new TreeViewModel() { Value = "Main Root" };
我使用这种方法填充了我的树结构:
public ObservableCollection<MessageElementViewModel> GetAllTreeNodes()
{
List<TreeViewModel> reqNodes = GetRequestTreeNodesFromModel(); // req
//List<TreeViewModel> respNodes = GetRequestTreeNodesFromModel(); // res
var request = new TreeViewModel();
var response = new TreeViewModel();
var parent = new TreeViewModel() { Value = "Main Root" };
for (int i = 0; i < wsMethodNameList.Count; i++)
{
var child = new TreeViewModel { Value = wsMethodNameList[i] };
parent.Children.Add(child);
var requestChild = new TreeViewModel { Value = requestXmlList[i] };
requestChild.Children = reqNodes;
child.Children.Add(requestChild);
var responseChild = new TreeViewModel { Value = responseXmlList[i] };
responseChild.Children = reqNodes; // change to response nodes
child.Children.Add(responseChild);
}
return new ObservableCollection<TreeViewModel> { parent};
xml
<TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding Path=MessageElements, Mode=TwoWay}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:MessageElementViewModel}" ItemsSource="{Binding Path= Children }">
<CheckBox IsChecked="{Binding Name}" Content="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
视图模型
public ObservableCollection<MessageElementViewModel> MessageElements
{
get { return messageElement; }
set
{
if (messageElement == value)
return;
messageElement = value;
NotifyPropertyChanged("MessageElements");
}
}
加载数据中
public void LoadData()
{
this.MessageElements = treeVModel.GetAllTreeNodes();
}
在我的 xaml 上,我想隐藏parent = new TreeViewModel() { Value = "Main Root" };
。
我怎样才能做到这一点?我试图给它一个空字符串,但它仍然显示展开图标。