0

我正在研究 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};

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

1 回答 1

0

ok got to go. but if u can get all items of your model you can do something like this:

ChildrenList =   Model.GetAll().Where(c => c.Parent != null);

cant do more without more information. Model.GetAll() should contain every parent and every child of your collection and with my example you can fish out the children only. theoretically... :P
good luck

于 2013-06-11T14:43:51.440 回答