使用多个数据源的数据绑定的WPF树视图

时间:2017-08-17 05:51:25

标签: wpf treeview

我有一个树视图,由2个不同的源(Mission,Target)使用,具体取决于GUI所处的模式。任何时候只显示1个源

课程信息:

任务等级

public class Mission
{
    public string Name { get; set; }
    public List<Target> PotentialTargets { get; set; }
    public List<Target> SelectedTargets { get; set; }
    public List<Operation> OpList { get; set; }
    //other properties
}

操作类

public class Operation
{
    public string Label { get; set; }
    //other properties
}

:定位

public class Target
{
    public string Label { get; set; }
    public TargetType Type { get; set; }
    public int Priority { get; set; }
    //other properties
}

的TargetType

public enum TargetType
{
    Star,
    Line
}

我正在尝试在将itemSource设置为其他来源时在TreeView上实现以下显示。

使命来源

- Mission_1(Name)
   - Potential_Target
       - Target_1(Label)
       - Target_2(Label)
   - Selected_Target
       - Target_1(Label)
   - Operation
       - Op_1(Name)
- Mission_2(Name)
   - Potential_Target
       - Target_1(Label)
       - Target_4(Label)
   - Selected_Target
       - Target_4(Label)
   - Operation
       - Op_1(Name)

目标来源

- Priority_1(Priority)
    - Star(TargetType)
       - Star_Target_1(Label)
    - Line(Type)
       - Line_Target_1(Label)
- Priority_2(Priority)
    - Star(TargetType)
       - Star_Target_2(Label)
       - Star_Target_3(Label)

所有帮助将不胜感激。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要一些基类来表示TreeView中的树节点。 例如,您可以声明这样的类:

<强> ABaseNode

// Base class to provide access to nodes Children
public abstract class ABaseNode
{
    private ObservableCollection<ABaseNode> children;

    public ObservableCollection<ABaseNode> Children
    {
        get { return children ?? (children = new ObservableCollection<ABaseNode>()); }
        set { children = value; }
    }
}

<强> LabeledNode

//Simple node with label to display
public class LabeledNode : ABaseNode
{
    public LabeledNode(string label)
    {
        Label = label;
    }

    public string Label { get; private set; }
}

任务等级

public class Mission : LabeledNode
{
    public Mission(string label) : base(label)
    {
        PotentialTargetsNode = new LabeledNode("PotentialTargets");
        Children.Add(PotentialTargetsNode);

        SelectedTargetsNode = new LabeledNode("SelectedTargets");
        Children.Add(SelectedTargetsNode);

        OpListNode = new LabeledNode("Operation");
        Children.Add(OpListNode);
    }

    public LabeledNode PotentialTargetsNode { get; private set; }
    public LabeledNode SelectedTargetsNode { get; private set; }
    public LabeledNode OpListNode { get; private set; }
}

操作类

public class Operation : LabeledNode
{
    public Operation(string label) : base(label)
    {
    }
    //other properties
}

目标类

public class Target : LabeledNode
{
    public Target(string label) : base(label)
    {
    }

    public TargetType Type { get; set; }
    public int Priority { get; set; }
    //other properties
}

PriorityNode类

public class PriorityNode : LabeledNode
{
    public PriorityNode(int priority) : base("Priority: " + priority)
    {
        StarTargetsNode = new LabeledNode("Star");
        Children.Add(StarTargetsNode);

        LineTargetsNode = new LabeledNode("Line");
        Children.Add(LineTargetsNode);
    }

    public LabeledNode StarTargetsNode { get; private set; }
    public LabeledNode LineTargetsNode { get; private set; }
}

MainWindow内容的Xaml

<StackPanel>
    <Button Content="Mission source" Click="MissionButtonClick"/>
    <Button Content="Target source" Click="TargetsButtonClick"/>
    <TreeView x:Name="treeView" Height="250" ItemsSource="{Binding Missions}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:LabeledNode}" ItemsSource="{Binding Path=Children}">
                <TextBlock Text="{Binding Path=Label}"/>
                <HierarchicalDataTemplate.ItemTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type local:LabeledNode}" ItemsSource="{Binding Children}">
                        <TextBlock Text="{Binding Label}"/>
                    </HierarchicalDataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</StackPanel>

数据初始化

public partial class MainWindow : Window
{
    public ObservableCollection<Mission> Missions { get; private set; }
    public ObservableCollection<PriorityNode> TargetsByPriority { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        var mission = new Mission("Mission1");
        mission.PotentialTargetsNode.Children.Add(new Target("Target1") {Type = TargetType.Line, Priority = 1});
        mission.PotentialTargetsNode.Children.Add(new Target("Target2") {Type = TargetType.Star, Priority = 2});
        mission.SelectedTargetsNode.Children.Add(new Target ("SelectedTarget") { Type = TargetType.Line, Priority = 1 });
        mission.OpListNode.Children.Add(new Operation("Op1"));

        Missions = new ObservableCollection<Mission> { mission };

        var priorityNode = new PriorityNode(1);

        priorityNode.StarTargetsNode.Children.Add(new Target("Target3") { Type = TargetType.Star, Priority = 1 });

        priorityNode.LineTargetsNode.Children.Add(new Target("Target1") { Type = TargetType.Line, Priority = 1 });
        priorityNode.LineTargetsNode.Children.Add(new Target("Target2") { Type = TargetType.Line, Priority = 1 });

        TargetsByPriority = new ObservableCollection<PriorityNode> { priorityNode};

        DataContext = this;
    }

    private void MissionButtonClick(object sender, RoutedEventArgs e)
    {
        treeView.ItemsSource = Missions;
    }

    private void TargetsButtonClick(object sender, RoutedEventArgs e)
    {
        treeView.ItemsSource = TargetsByPriority;
    }
}

现在点击按钮即可更改TreeView ItemsSource