回复 2楼swejet的帖子
可以添加 .NET 标准的 Combox 到 单元格中模拟这种效果:
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- this.gcSpreadSheet1.Sheets.Clear();
- this.gcSpreadSheet1.Sheets.Add(new MyWorksheet() { RowCount = 10, ColumnCount = 8, DefaultColumnWidth = 100, DefaultRowHeight = 28 });
- var sheet = this.gcSpreadSheet1.ActiveSheet;
- sheet.SetValue(0, 0, "General");
- sheet.SetValue(0, 1, "TextBox");
- sheet.SetValue(0, 2, "Slider");
- sheet.SetValue(0, 3, "DatePicker");
- sheet.SetValue(0, 4, "CheckBox");
- sheet.SetValue(0, 5, "ProgressBar");
- sheet.SetValue(0, 6, "ComboBox");
- sheet.SetValue(0, 7, "Button");
- for (var i = 1; i < 10; i++)
- {
- sheet.SetValue(i, 2, i);
- sheet.SetValue(i, 3, DateTime.Now + TimeSpan.FromDays(i * 365));
- sheet.SetValue(i, 4, i % 2 == 0 ? true : false);
- sheet.SetValue(i, 5, i);
- sheet.SetValue(i, 6, i % 3);
- }
- var sheet1 = this.gcSpreadSheet1.ActiveSheet as MyWorksheet;
- if (sheet != null)
- {
- sheet1.DrawingObjectVisible = true;
- this.gcSpreadSheet1.InvalidateRange(-1, -1, -1, -1);
- }
- }
- }
- public class ControlDrawingObject : CustomDrawingObject
- {
- private Control _rootElement;
- public ControlDrawingObject(int row, int col, Control control) : base(row, col) { _rootElement = control; this.ShowDrawingObjectOnly = true; }
- public override FrameworkElement RootElement
- {
- get { _rootElement.Margin = new Thickness(1); return _rootElement; }
- }
- }
- public class MyWorksheet : Worksheet
- {
- public bool DrawingObjectVisible { get; set; }
- public override DrawingObject[] GetDrawingObject(int row, int column, int rowCount, int columnCount)
- {
- if (row == 0 || !this.DrawingObjectVisible) return base.GetDrawingObject(row, column, rowCount, columnCount);
- DrawingObject dobj;
- if (column == 7)
- {
- dobj = new ControlDrawingObject(row, column, new Button() { Content = "Delete" });
- }
- else if (column == 6)
- {
- int value = -1;
- object obj = this.GetValue(row, column);
- if (obj != null)
- {
- if (obj is int) value = (int)obj;
- else
- {
- int.TryParse(obj.ToString(), out value);
- }
- if (value > 2) value = -1;
- }
- ComboBox control = new ComboBox();
- control.Items.Add("Fruit");
- control.Items.Add("Vegetable");
- control.Items.Add("Food");
- if (value >= -1 && value < control.Items.Count)
- control.SelectedIndex = value;
- control.SelectionChanged += delegate(object sender, SelectionChangedEventArgs e)
- {
- this.SetValue(row, column, control.SelectedIndex);
- };
- dobj = new ControlDrawingObject(row, column, control);
- }
- else if (column == 5)
- {
- double value = 0;
- object obj = this.GetValue(row, column);
- if (obj != null)
- {
- if (obj is double) value = (double)obj;
- else if (obj is int) value = (int)obj;
- else
- {
- double.TryParse(obj.ToString(), out value);
- }
- }
- dobj = new ControlDrawingObject(row, column, new ProgressBar() { Value = value, Maximum = 10 });
- }
- else if (column == 4)
- {
- bool value = false;
- object obj = this.GetValue(row, column);
- if (obj != null)
- {
- if (obj is bool) value = (bool)obj;
- else
- {
- bool.TryParse(obj.ToString(), out value);
- }
- }
- CheckBox control = new CheckBox() { IsChecked = value, Content = "Check Me" };
- control.Checked += delegate(object sender, RoutedEventArgs e)
- {
- this.SetValue(row, column, control.IsChecked);
- };
- control.Unchecked += delegate(object sender, RoutedEventArgs e)
- {
- this.SetValue(row, column, control.IsChecked);
- };
- dobj = new ControlDrawingObject(row, column, control);
- }
- else if (column == 3)
- {
- DateTime? value = null;
- object obj = this.GetValue(row, column);
- if (obj != null)
- {
- if (obj is DateTime) value = (DateTime)obj;
- else
- {
- DateTime date;
- if (DateTime.TryParse(obj.ToString(), out date))
- {
- value = date;
- }
- }
- }
- DatePicker control = new DatePicker() { SelectedDate = value };
- control.LostFocus += delegate(object sender, RoutedEventArgs e)
- {
- this.SetValue(row, column, control.SelectedDate);
- };
- dobj = new ControlDrawingObject(row, column, control);
- }
- else if (column == 2)
- {
- double value = 0;
- object obj = this.GetValue(row, column);
- if (obj != null)
- {
- if (obj is double) value = (double)obj;
- else if (obj is int) value = (int)obj;
- else
- {
- double.TryParse(obj.ToString(), out value);
- }
- }
- Slider control = new Slider() { Value = value, Maximum = 10 };
- control.LostFocus += delegate(object sender, RoutedEventArgs e)
- {
- this.SetValue(row, column, control.Value);
- };
- dobj = new ControlDrawingObject(row, column, control);
- }
- else if (column == 1)
- {
- string value = this.GetText(row, column);
- TextBox control = new TextBox() { Text = value };
- control.LostFocus += delegate(object sender, RoutedEventArgs e)
- {
- this.SetValue(row, column, control.Text);
- };
- dobj = new ControlDrawingObject(row, column, control);
- }
- else
- {
- return base.GetDrawingObject(row, column, rowCount, columnCount);
- }
- return new DrawingObject[] { dobj };
- }
- }
复制代码 |