コード例 #1
0
        public static void BindSelectedItems(this ListView control, ListField <ListViewRow> field)
        {
            bool ignore = false;

            control.SelectedIndexChanged += (sender, e) =>
            {
                ignore = true;
                try
                {
                    field.Set(control.SelectedItems.Cast <ListViewItem>().Select(x => x.Tag).Cast <ListViewRow>());
                }
                finally
                {
                    ignore = false;
                }
            };

            field.Changed += (sender, e) =>
            {
                // ignore is true when the ListField has changed as the result of the user selecting an item.  Therefore,
                // we don't want to redo that same change on the control.
                if (ignore)
                {
                    return;
                }

                control.BeginUpdate();
                try
                {
                    control.SelectedItems.Clear();
                    foreach (var row in field)
                    {
                        foreach (var item in control.Items.Cast <ListViewItem>())
                        {
                            if (item.Tag == row)
                            {
                                item.Selected = true;
                                break;
                            }
                        }
                    }
                }
                finally
                {
                    control.EndUpdate();
                }
            };
        }
コード例 #2
0
        public static void BindItems(this ComboBox control, ListField <string> field)
        {
            control.Items.AddRange(field.ToArray());

            field.Changed += (sender, e) =>
            {
                int selectedIndex = control.SelectedIndex;
                control.BeginUpdate();
                try
                {
                    control.Items.Clear();
                    control.Items.AddRange(field.ToArray());
                    if (control.Items.Count > 0)
                    {
                        control.SelectedIndex = Math.Max(0, Math.Min(control.Items.Count - 1, selectedIndex));
                    }
                }
                finally
                {
                    control.EndUpdate();
                }
            };
        }
コード例 #3
0
        private static void PopulateListView(ListView control, ListField <ListViewRow> field)
        {
            var groups =
                field
                .Select(x => x.GroupName)
                .Where(x => x != null)
                .Distinct()
                .OrderBy(x => x)
                .ToDictionary(
                    x => x,
                    x => new ListViewGroup(x, x));
            var images =
                field
                .Select(x => x.Icon)
                .Where(x => x != null)
                .Distinct()
                .Zip(Enumerable.Range(0, int.MaxValue), (image, index) => new { image, index })
                .ToDictionary(x => x.image, x => x.index);

            if (images.Any())
            {
                var imageList = new ImageList();
                foreach (var pair in images)
                {
                    imageList.Images.Add(pair.Key);
                }
                if (control.SmallImageList != null)
                {
                    control.SmallImageList.Dispose();
                }
                control.SmallImageList = imageList;
            }

            var boldFont = new Font(control.Font, FontStyle.Bold);

            control.BeginUpdate();
            try
            {
                var selectedIndices = control.SelectedIndices.Cast <int>().ToList();

                control.Groups.Clear();
                control.Items.Clear();

                control.Groups.AddRange(groups.Values.ToArray());

                foreach (var row in field)
                {
                    var lvi =
                        row.GroupName == null
                  ? new ListViewItem()
                  : new ListViewItem(groups[row.GroupName]);

                    lvi.Tag  = row;
                    lvi.Font = boldFont;
                    lvi.UseItemStyleForSubItems = false;

                    bool first = true;
                    foreach (string cell in row.Cells)
                    {
                        if (first)
                        {
                            lvi.Text = cell;
                        }
                        else
                        {
                            lvi.SubItems.Add(cell);
                            lvi.SubItems.Cast <ListViewItem.ListViewSubItem>().Last().Font = control.Font;
                        }
                        first = false;
                    }

                    if (row.Icon != null)
                    {
                        lvi.ImageIndex = images[row.Icon];
                    }

                    control.Items.Add(lvi);
                }

                control.SelectedIndices.Clear();
                foreach (int index in selectedIndices.Where(x => x < control.Items.Count))
                {
                    control.SelectedIndices.Add(index);
                }
            }
            finally
            {
                control.EndUpdate();
            }
        }
コード例 #4
0
        public static void BindItems(this ListView control, ListField <ListViewRow> field)
        {
            PopulateListView(control, field);

            field.Changed += (sender, e) => PopulateListView(control, field);
        }