如何隐藏树的第二个根

时间:2013-06-11 13:44:29

标签: wpf mvvm

我正在研究WPF TreeView,我想要隐藏树的父级。

我的结构是Main Root --> WSMethodName (2nd root) --> request and response sub children

在我的xaml上,我可以像parent = new TreeViewModel() { Value = "Main Root" };一样隐藏ItemsSource="{Binding MessageElements[0].Children}"

如何隐藏wsMethodNameList,它是包含请求和响应对象的树的第二个根?

对于这种情况,父母是:

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();
}

1 个答案:

答案 0 :(得分:0)

好吧得走了。但如果你能得到你模特的所有物品,你可以这样做:

ChildrenList =   Model.GetAll().Where(c => c.Parent != null);
如果没有更多信息,就无法做更多事情。 Model.GetAll()应该包含您收藏的每个父母和每个孩子,并且我的例子中您只能捕捉孩子。理论上......:P
祝你好运