单元格类型
你好!怎么设置单元格是button类型,并且button只占单元格的一部分??您好,您用的是具体哪个平台的Spread ,可否贴出您button 的代码 dexteryao 发表于 2017-3-7 10:21
您好,您用的是具体哪个平台的Spread ,可否贴出您button 的代码
你好,是spread for silverlight,我是看你们的例子里可以设置button类型,但是button把单元格全部覆盖了,能不能设置成只覆盖单元格的一部分 dexteryao 发表于 2017-3-7 10:21
您好,您用的是具体哪个平台的Spread ,可否贴出您button 的代码
public class MyWorksheet : GrapeCity.Windows.SpreadSheet.Data.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 = "..."});
}
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 };
}
}
单元格获取焦点的时候,button显示,失去焦点时,button隐藏 给button设置 margin
dobj = new ControlDrawingObject(row, column, new Button() { Content = "Delete", Margin = new Thickness(5, 5, 5, 5) });
然后修改下
public override FrameworkElement RootElement
{
get
{
if (_rootElement.Margin.Left == 0)
{
_rootElement.Margin = new Thickness(1);
}
return _rootElement;
}
} dexteryao 发表于 2017-3-7 17:05
给button设置 margin
dobj = new ControlDrawingObject(row, column, new Button() { Content = " ...
非常感谢!!
还有一个问题,怎样在单元格获取焦点的时候显示button,失去焦点的时候隐藏button呢 可以根据不同情况绘制button
if (column == 7)
{
if (this.ActiveColumnIndex == column && this.ActiveRowIndex == row)
{
dobj = new ControlDrawingObject(row, column, new Button() { Content = "Delete", Margin = new Thickness(5, 5, 5, 5) });
}
else
{
return base.GetDrawingObject(row, column, rowCount, columnCount);
}
}
更新页面
this.gcSpreadSheet1.EnterCell += GcSpreadSheet1_EnterCell;
this.gcSpreadSheet1.LeaveCell += GcSpreadSheet1_LeaveCell;
}
private void GcSpreadSheet1_LeaveCell(object sender, LeaveCellEventArgs e)
{
if (e.Column == 7)
{
this.gcSpreadSheet1.InvalidateRange(-1, -1, -1, -1);
}
}
private void GcSpreadSheet1_EnterCell(object sender, EnterCellEventArgs e)
{
if (e.Column == 7)
{
this.gcSpreadSheet1.InvalidateRange(-1, -1, -1, -1);
}
} dexteryao 发表于 2017-3-7 18:15
可以根据不同情况绘制button
if (column == 7)
{
你好,不好意思,现在还有一个问题,就是设置一个单元格button类型后,所有sheet的这个单元格都变成了button类型,这个怎么解决,代码如下:
class MyDrawingObjectProvider : IDrawingObjectProvider
{
public DrawingObject[] GetDrawingObjects(Worksheet sheet, int row, int column, int rowCount, int columnCount)
{
DrawingObject dobj;
if (row == zx_decompose.Rowindex && column == zx_decompose.Colindex)
{
Button bt = new Button() { Content = "...", Width = 20, Margin = new Thickness(sheet.ActiveColumn.Width - 20.0, 0, 0, 0) };
bt.Click += control_Click;
dobj = new ControlDrawingObject(row, column, bt);
return new DrawingObject[] { dobj };
}
else
{
return null;
}
}
void control_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("哈哈!成功了!!o⁺46x5");
}
}
public class ControlDrawingObject : GrapeCity.Windows.SpreadSheet.UI.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
{
if (_rootElement.Margin.Left == 0)
{
_rootElement.Margin = new Thickness(1);
}
return _rootElement;
}
}
} 这个应该是您逻辑问题,判断的地方有问题。具体需要您调试下
另外,不要return null 。
return
return base.GetDrawingObject(row, column, rowCount, columnCount);