WPF不显示一个集合

本文关键字:一个 集合 显示 WPF | 更新日期: 2023-09-27 18:03:02

我有两个可观察的集合,我的视图绑定到,一个被称为网络。节点和one it网络。连接——网络对象是我视图模型中的一个属性。

我正在从BackgroundWorker获取数据,并通过BW.ProgressChanged添加新的节点'连接。

当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"); 
        }
    }

网络视图模型有两个可观察集合,一个是NodeView模型,一个是ConnectionViewModel。

Connection View模型:

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
}

}

WPF不显示一个集合

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

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

Dispatcher可以在所有ui - element上找到。不确定你的代码是如何结构的,所以不知道从哪里得到一个没有看到更多的代码或获得更多的信息。