请问WPF Spread中能不能像以前版本那样将一列类型设置成按钮
请问WPF Spread中能不能像以前版本那样将一列类型设置成按钮,或者CheckBox 回复 1楼gaoge00的帖子目前产品暂不支持这个功能,不过有一个替代方案:
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 };
}
}
回复 2楼Zoe的帖子
谢谢,表头的文字能不能竖着显示?
这个我自己想办法解决了,把这列设置成自动折行,然后在列头文字中添加\r\n换行,好在我这些列的下面都是CheckBox,如果不是的话这个方法就不能实现了,不然下面表中的文字也换行了。。。 你可以设置文字是否要换行。如果把WordWrap 设置成false, 就不会换行了。
问题解决了就好。:jy74a: 回复 4楼Zoe的帖子
哈哈, 我发现我的问题好多,现在有个 回车跳格的问题,
以前的是这样写的,现在的就不会了。。
InputMap im;
im = ospd.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenAncestorOfFocused);
im.Put(new FarPoint.Win.Spread.Keystroke(Keys.Enter, Keys.None), FarPoint.Win.Spread.SpreadActions.MoveToNextColumnWrap);
im = ospd.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused);
im.Put(new FarPoint.Win.Spread.Keystroke(Keys.Enter, Keys.None), FarPoint.Win.Spread.SpreadActions.MoveToNextColumnWrap);
this.gcSpreadSheet1.View.KeyMap.Remove(new KeyStroke(Key.Enter, ModifierKeys.None));
this.gcSpreadSheet1.View.KeyMap.Add(new KeyStroke(Key.Enter, ModifierKeys.None), SpreadActions.NavigationNext);
回复 2楼Zoe的帖子
您好,又麻烦您了, 这种方式的实现可不可以写成 spd.ActiveSheet.Columns.XX=new Text();这种形式,因为有些列我还要用原有的类型, 不这样的话很可能整个表都需要重新来定义。。 产品目前不支持这个功能,可能需要重新定义了。在后续版本的开发中,我们会考虑添加这个功能。 回复 8楼Zoe的帖子
谢谢,希望推出新的版本
现在有个问题 如果我New一个Control类型就可以绑定到该列
但是如果我用穿过来的Control 却只能绑定在这一列的最后,请问这是什么原因?
这是New的正常
Button btnDelete = new Button() { Content = "Delete" };
这是我传过来的 Control 只有最后一行能绑定上
Button btnDelete = listControl.MyControlType as Button;
--------------------------------------------------------------------------------------
下面正常
dobj = new ControlDrawingObject(row, column, btnDelete);
return new DrawingObject[] { dobj };
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
追加询问:
现有的列类型 如何限制长度以及录入的类型? 抱歉,您说的这个问题,能否提供简单的demo 方便我们找出问题。