WPF没有显示一个集合

时间:2014-05-13 14:09:05

标签: c# wpf backgroundworker

我有两个可观察的集合,我的视图绑定到,一个称为Network.Nodes,一个称为Network.Connections - 网络对象是我视图模型中的一个属性。

我从BackgroundWorker获取数据并通过BW.ProgressChanged添加新的Node \ Connection。

当BW完成它的工作时,我只看到节点而没有连接。我添加了一个Debug.WriteLine(" Network.Connection.Cout =" + Network.Connection.Cout); 在BW完成工作,我发现内部有2个元素(这是正确的数量),但由于某种原因,我没有看到元素。

ProgressChanged:

if (e.ProgressPercentage == 2)      //Add connection to UI
        {
            ConnectionViewModel tempCon = new ConnectionViewModel();
            List<Object> ConnectionObj = new List<object>();
            ConnectionObj = (List<Object>)e.UserState;
            tempCon.SourceConnector = (ConnectorViewModel)ConnectionObj[0];
            tempCon.DestConnector = (ConnectorViewModel)ConnectionObj[1];
            Network.Connections.Add(tempCon);

这就是我调用ProgressChange的方式:

                        List<Object> connectionObj = new List<Object>();

                        connectionObj.Add(connection.SourceConnector);
                        connectionObj.Add(connection.DestConnector);
                        connectionObj.Add(connection.Type);
                        connectionObj.Add(i++);

                        bw.ReportProgress(2, connectionObj);

绑定:

     NodesSource="{Binding Network.Nodes}"
     ConnectionsSource="{Binding Network.Connections}"

视图模型中的网络:

  public NetworkViewModel Network
    {
        get
        {
                return network;
        }
        set
        {
                network = value;

                OnPropertyChanged("Network"); 
        }
    }

网络视图模型有两个observableCollections,一个是NodeView模型,一个是ConnectionViewModel。

连接视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows;

namespace YogevAfekaRAPAT.YNIDS.ViewModels
{
/// <summary>
/// Defines a connection between two connectors (aka connection points) of two nodes.
/// </summary>
public sealed class ConnectionViewModel : AbstractModelBase
{
    #region Internal Data Members

    /// <summary>
    /// The source connector the connection is attached to.
    /// </summary>
    private ConnectorViewModel sourceConnector = null;

    /// <summary>
    /// The destination connector the connection is attached to.
    /// </summary>
    private ConnectorViewModel destConnector = null;

    /// <summary>
    /// The source and dest hotspots used for generating connection points.
    /// </summary>
    private Point sourceConnectorHotspot;
    private Point destConnectorHotspot;

    /// <summary>
    /// Points that make up the connection.
    /// </summary>
    private PointCollection points = null;

    #endregion Internal Data Members

    public enum ConnectorType
    {
        REGULAR = 0,
        FLOW = 1
    }

    /// <summary>
    /// The source connector the connection is attached to.
    /// </summary>
    public ConnectorViewModel SourceConnector
    {
        get
        {
            return sourceConnector;
        }
        set
        {
            if (sourceConnector == value)
            {
                return;
            }

            if (sourceConnector != null)
            {
                sourceConnector.AttachedConnections.Remove(this);
                sourceConnector.HotspotUpdated -= new EventHandler<EventArgs>(sourceConnector_HotspotUpdated);
            }

            sourceConnector = value;

            if (sourceConnector != null)
            {
                sourceConnector.AttachedConnections.Add(this);
                sourceConnector.HotspotUpdated += new EventHandler<EventArgs>(sourceConnector_HotspotUpdated);
                this.SourceConnectorHotspot = sourceConnector.Hotspot;
            }

            OnPropertyChanged("SourceConnector");
            OnConnectionChanged();
        }
    }

    /// <summary>
    /// The destination connector the connection is attached to.
    /// </summary>
    public ConnectorViewModel DestConnector
    {
        get
        {
            return destConnector;
        }
        set
        {
            if (destConnector == value)
            {
                return;
            }

            if (destConnector != null)
            {
                destConnector.AttachedConnections.Remove(this);
                destConnector.HotspotUpdated -= new EventHandler<EventArgs>(destConnector_HotspotUpdated);
            }

            destConnector = value;

            if (destConnector != null)
            {
                destConnector.AttachedConnections.Add(this);
                destConnector.HotspotUpdated += new EventHandler<EventArgs>(destConnector_HotspotUpdated);
                this.DestConnectorHotspot = destConnector.Hotspot;
            }

            OnPropertyChanged("DestConnector");
            OnConnectionChanged();
        }
    }

    /// <summary>
    /// The source and dest hotspots used for generating connection points.
    /// </summary>
    public Point SourceConnectorHotspot
    {
        get
        {
            return sourceConnectorHotspot;
        }
        set
        {
            sourceConnectorHotspot = value;

            ComputeConnectionPoints();

            OnPropertyChanged("SourceConnectorHotspot");
        }
    }

    public Point DestConnectorHotspot
    {
        get
        {
            return destConnectorHotspot;
        }
        set
        {
            destConnectorHotspot = value;

            ComputeConnectionPoints();

            OnPropertyChanged("DestConnectorHotspot");
        }
    }

    /// <summary>
    /// Points that make up the connection.
    /// </summary>

    public PointCollection Points
    {
        get
        {
            return points;
        }
        set
        {
            points = value;

            OnPropertyChanged("Points");
        }
    }


    private ConnectorType type;
    public ConnectorType Type
    {
        get
        {
            return type;
        }
        set
        {
            type = value;

            OnPropertyChanged("Type");
        }
    }

    /// <summary>
    /// Event fired when the connection has changed.
    /// </summary>
    public event EventHandler<EventArgs> ConnectionChanged;

    #region Private Methods

    /// <summary>
    /// Raises the 'ConnectionChanged' event.
    /// </summary>
    private void OnConnectionChanged()
    {
        if (ConnectionChanged != null)
        {
            ConnectionChanged(this, EventArgs.Empty);
        }
    }


    #endregion Private Methods

}

}

1 个答案:

答案 0 :(得分:0)

我不确定,但可能是您尝试从后台工作程序更新集合,这可能会导致问题。您需要使用Dispatcher将其封送到UI-Thread,如:

view.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            Network.Connections.Add(tempCon);
                        }), DispatcherPriority.Normal);

可以在所有UI元素上找到Dispatcher。不确定你的代码是如何构造的,因此无法告诉从何处获取代码而无需查看更多代码或获取更多信息。