如何把单元格类型改变为COMBOBOX
现在想把单元格改变为下拉框的样式,设计器中没发现有这样的功能,找到的例子中要重写GetDrawingObject,没弄明白,该怎么弄才能把单元格变成COMBOBOX的样子 回复 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 };
}
}
我是直接打开EXCEL文件进行编辑,如果Clear掉所有Sheet数据不就丢失了嘛,有没有别的解决方法啊,在原有的Sheet中添加Combobox 回复 3楼iceman的帖子
我是直接打开EXCEL文件进行编辑,如果Clear掉所有Sheet数据不就丢失了嘛,有没有别的解决方法啊,在原有的Sheet中添加Combobox 回复 5楼swejet的帖子
3# 的例子用于说明可以嵌入 Combo 到Cell中,当然您在设置之前也可以不清除表单,直接设置单元格类型即可。 回复 6楼iceman的帖子
Silverlight怎直接设置单元格类型?具体例子代码有吗? 回复 7楼swejet的帖子
抱歉,让您久等了。
和您确认下需求,目前希望在导入 Excel 后,某个单元格保留原来的文本,但是需要设置单元格类型为 Combo 对吗?
DrawingObject 是绘制到单元格中的,如果单元格之前存在文本则需要把当前单元格value添加到 Combo 文本中。
在确认您的需求后我这边会尝试制作 Demo 上传。
谢谢 回复 8楼iceman的帖子
我用的是Silverlight平台,单元格可能为空也可能不为空,但是要的是在原有的Sheet上添加。希望能解决 回复 9楼swejet的帖子
我们正在沟通开发人员问您制作这个 demo,完成后会第一时间发送给您。谢谢
页:
[1]
2