我正在研究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};
XAML
<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" };
。
我怎样才能实现这一目标?我试图给它一个空字符串,但它仍然显示展开图标。
答案 0 :(得分:2)
只是不要绑定根本身,而是它的子节点。