从TreeView选择中撤消对象类型的最佳方法是什么?

时间:2019-04-03 17:52:44

标签: c# wpf

我正在用不同的对象类型填充TreeView。根据选择的项目,我需要在视图模型中设置一个属性或另一个属性。

我试图在SelectedItemChanged事件中捕获选择,但是没有看到期望的结果。

为了进行测试,我创建了3个不同的类:

public static int ShortestDistance(Graph graph, Node from, Node to)
{
    var distances = new Dictionary<Node, int>();
    var actualNodes = graph.GetNodes() as List<Node> ?? Graph.GetNodes().ToList();

    foreach (var node in actualNodes) distances[node] = node.Equals(from) ? 0 : int.MaxValue;

    while (actualNodes.Count() != 0)
    {
        var actualShortest = actualNodes.OrderBy(n => distances[n]).First();
        actualNodes.Remove(actualShortest);

        if (distances[actualShortest] == int.MaxValue) break;

        if (actualShortest.Equals(to)) return distances[actualShortest];

        foreach (var adjacent in graph.GetAdjacentsByNode(actualShortest))
        {
            var actualDistance = distances[actualShortest] + adjacent.Weight;
            if (actualDistance >= distances[adjacent.To]) continue;
            distances[adjacent.To] = actualDistance;
        }
    }

    throw new Exception($"There's no such route from '{from}' to '{to}'.");
}

然后我每年一次填充树视图:

   public class Cuadro
{
    public string Nombre { get; set; }
    public List<Circuito> Circuitos { get; set; }
}

public class Circuito
{
    public string NombreCircuito { get; set; }
    public List<Camino> Caminos { get; set; }
}

public class Camino
{
    public string Ruta { get; set; }
}

0 个答案:

没有答案