简体   繁体   中英

WPF not showing one collection

i have two observable Collections that my view is binded to, one is called Network.Nodes and one it Network.Connections - the Network object is a Property in my view model.

I'm getting data from BackgroundWorker and adding new Node\\Connection via the BW.ProgressChanged.

When the BW finish it's work i see only nodes and no connection. i added a Debug.WriteLine("Network.Connection.Cout = " + Network.Connection.Cout); On the BW finish work, and i see that there are 2 elements inside (which is the correct amount) but for some reason i don't see the elements.

The 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);

This is how i call the 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);

The binding:

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

The Network in the View Model:

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

                OnPropertyChanged("Network"); 
        }
    }

The Network View Model has two observableCollections one is of type NodeView model and one is ConnectionViewModel.

the Connection View model:

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

}

}

I am not sure, but it might be that you're trying to update the collections from the background worker, which can result in problems. You need to marshal that to the UI-Thread using a Dispatcher like:

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

A Dispatcher can be found on all UI-Elements. Not sure how your code is structured so can't tell where to get one from without seeing more code or getting more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM