从列表视图中的组合框获取数据 - c#

时间:2015-12-14 09:05:29

标签: c# wpf listview foreach combobox

我想从列表框中的combobox中获取所选项目,并使用foreach从WPF中的listview中获取列中的id:

此代码用于生成我的组合框项目:

plotChart

我从其他数据转换xml数据......

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Plot
{
    public partial class XYPlot : Form
    {
        #region constructors

        public string Title
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = "XYPlot : " + value;
            }
        }

        //default, used for multiplots
        public XYPlot()
        {
            InitializeComponent();
            InitializeChartStyles();
        }

        private void InitializeChartStyles()
        {
            plotChart.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;

            plotChart.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
            plotChart.ChartAreas["ChartArea1"].AxisX.MinorGrid.Enabled = false;
            plotChart.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;
            plotChart.ChartAreas["ChartArea1"].AxisY.MinorGrid.Enabled = false;
        }

        //one-offs, used for simple plots of data
        public XYPlot(string name, Series series)
        {
            InitializeComponent();
            InitializeChartStyles();
            this.Add(series);
            this.Show();
        }

        public XYPlot(string name, DataPointCollection XYpoints)
        {
            InitializeComponent();
            InitializeChartStyles();
            this.Add(name, XYpoints);
            this.Show();
        }

        public XYPlot(string name, List<Tuple<double, double>> XYpoints)
        {
            InitializeComponent();
            InitializeChartStyles();
            this.Add(name, XYpoints); 
            this.Show();
        }

        public XYPlot(string name, List<double> X, List<double> Y)
        {
            InitializeComponent();
            InitializeChartStyles();
            this.Add(name, X, Y);
            this.Show();
        }

        public XYPlot(string name, List<double> Y, double xIncrements = 1.0, double xOffset = 0.0)
        {
            InitializeComponent();
            InitializeChartStyles();
            this.Add(name, Y, xIncrements);
            this.Show();
        }

        private void FormPlot_Load(object sender, EventArgs e)
        {
            Clear();
        }

        #endregion

        #region getNextDefault
        private List<Color> usedColors = new List<Color>();
        private Color getNextDefaultColor()
        {
            switch(usedColors.Count)
            {
                case 1:
                    return Color.Red;

                case 2:
                    return Color.Green;

                case 3:
                    return Color.Black;

                default:
                    return Color.Blue;
            }
        }

        private Series newDefaultSeries(string Name)
        {
            var series = new System.Windows.Forms.DataVisualization.Charting.Series
            {
                Name = Name,
                BorderWidth = 2,
                Color = getNextDefaultColor(),
                IsVisibleInLegend = true,
                IsXValueIndexed = false,
                ChartType = SeriesChartType.Line
            };
            usedColors.Add(series.Color);
            return series;
        }
        #endregion

        #region public methods

        public void Clear()
        {
            plotChart.Series.Clear();
            usedColors.Clear();
        }

        public Series getSeries(string Name)
        {
            return plotChart.Series[Name];
        }

        #region Add
        public void Add(string Name, DataPointCollection XYpoints)
        {
            Series series = newDefaultSeries(Name);
            plotChart.Series.Add(series);

            //shallow copy
            foreach (DataPoint p in XYpoints)
            {
                series.Points.Add(p);
            }

            //invalidates the old surfcace, and thus requests a redraw
            plotChart.Refresh();
        }

        public void Add(string Name, List<Tuple<double, double>> XYpoints)
        {
            Series series = newDefaultSeries(Name);
            plotChart.Series.Add(series);

            //shallow copy
            foreach (Tuple<double, double> XY in XYpoints)
            {
                series.Points.AddXY(XY.Item1, XY.Item2);
            }

            //invalidates the old surfcace, and thus requests a redraw
            plotChart.Refresh();
        }

        public void Add(string Name, List<double> X, List<double> Y)
        {
            Series series = newDefaultSeries(Name);
            plotChart.Series.Add(series);

            if (X.Count != Y.Count)
                throw new Exception("X and Y vectors must be of same length, otherwise I cannot plot them in an XY plot!");

            //shallow copy
            for (int i = 0; i < X.Count; i++)
            {
                series.Points.AddXY(X[i], Y[i]);
            }

            //invalidates the old surfcace, and thus requests a redraw
            plotChart.Refresh();
        }

        public void Add(string Name, List<double> Y, double xIncrements = 1.0, double xOffset = 0.0)
        {
            Series series = newDefaultSeries(Name);
            plotChart.Series.Add(series);

            //shallow copy
            if (Y != null)
            {
                for (int i = 0; i < Y.Count; i++)
                {
                    double x = ((double)i) * xIncrements + xOffset;
                    series.Points.AddXY(x, Y[i]);
                }
            }

            //invalidates the old surface, and thus requests a redraw
            plotChart.Refresh();

        }

        public void Add(System.Windows.Forms.DataVisualization.Charting.Series series)
        {
            plotChart.Series.Add(series);
            plotChart.Refresh();
        }
        #endregion

        #region updateSeries
        public void updateSeries(string Name, DataPointCollection XYpoints)
        {
            try
            {
                if (plotChart.Series[Name] == null)
                    Add(Name, XYpoints);
            }
            catch
            {
                Add(Name, XYpoints);
            }

            //shallow copy
            foreach (DataPoint p in XYpoints)
            {
                plotChart.Series[Name].Points.Add(p);
            }

            //invalidates the old surfcace, and thus requests a redraw
            plotChart.Refresh();
        }

        public void updateSeries(string Name, List<Tuple<double, double>> XYpoints)
        {
            try
            {
                if (plotChart.Series[Name] == null)
                    Add(Name, XYpoints);
            }
            catch
            {
                Add(Name, XYpoints);
            }

            //shallow copy
            foreach (Tuple<double, double> XY in XYpoints)
            {
                plotChart.Series[Name].Points.AddXY(XY.Item1, XY.Item2);
            }

            //invalidates the old surfcace, and thus requests a redraw
            plotChart.Refresh();
        }

        public void updateSeries(string Name, List<double> X, List<double> Y)
        {
            try
            {
                if (plotChart.Series[Name] == null)
                    Add(Name, X, Y);
            }
            catch
            {
                Add(Name, X, Y);
            }

            plotChart.Series[Name].Points.Clear();

            if (X.Count != Y.Count)
                throw new Exception("X and Y vectors must be of same length, otherwise I cannot plot them in an XY plot!");

            //shallow copy
            for (int i = 0; i < X.Count; i++)
            {
                plotChart.Series[Name].Points.AddXY(X[i], Y[i]);
            }

            //invalidates the old surface, and thus requests a redraw
            plotChart.Refresh();

        }

        public void updateSeries(string Name, List<double> Y, double xIncrements = 1.0, double xOffset = 0.0)
        {
            try
            {
                if (plotChart.Series[Name] == null)
                    Add(Name, Y, xIncrements, xOffset);
            }
            catch
            {
                Add(Name, Y, xIncrements, xOffset);
            }

            plotChart.Series[Name].Points.Clear();

            //shallow copy
            if (Y != null)
            {
                for (int i = 0; i < Y.Count; i++)
                {
                    double x = ((double)i) * xIncrements + xOffset;
                    plotChart.Series[Name].Points.AddXY(x, Y[i]);
                }
            }

            //invalidates the old surface, and thus requests a redraw
            plotChart.Refresh();

        }

        public void updateSeries(System.Windows.Forms.DataVisualization.Charting.Series series)
        {
            try
            {
                if (plotChart.Series[series.Name] == null)
                    Add(series);
            }
            catch
            {
                Add(series);
            }

            plotChart.Series[series.Name] = series;
            plotChart.Refresh();
        }
        #endregion

        #endregion
    }
}

WPF中的我的Windows资源。

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace ICF_Form
{
    public enum EnumLimitation
    {
        Aucune,
        Légère,
        Modérée,
        Forte,
        Totale
    }

    public class Limitation : INotifyPropertyChanged
    {
        public int _id { get; set; }

        private EnumLimitation _enumLimitation = EnumLimitation.Modérée;
        public EnumLimitation _limitation
        {
            get { return _enumLimitation; }
            set
            {
                _enumLimitation = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Limitation"));
            }
        }
        public Limitation() { }
        public Limitation(int id = 0)
        {
            _id = id;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class Limitations : ObservableCollection<Limitation>
    {
        public override string ToString()
        {
            string s = "";
            foreach (var item in this)
            {
                s += item.ToString() + "\n";
            }
            return s;
        }
    }
}

我只想为listview的所有行获取所有数据(ID和选定的ComboboxItem)。我不需要CRUD或其他。只需存储变量{ID:x;选择组合框项目:y}

其实我只有那个:

using System;
using System.Windows.Data;
using System.Xml;

namespace ICF_Form
{
    class XConverter : IValueConverter
    {
        /// <summary>
        /// Construction du converter
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string s = null;
            if (value is XmlElement)
                s = (value as XmlElement).InnerText;
            if (value is string)
                s = (string)value;
            if (s != null)
                return Enum.Parse(typeof(EnumLimitation), s);
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}

0 个答案:

没有答案