DevExpress中 的DataGrid每一行根据其类型显示控件的种类

public class ValueSelector : DataTemplateSelector
{
public ValueSelector()
{
}
bool IsScrollMove = false;
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
DataTemplate dt = new DataTemplate();
EditGridCellData cellDatas = item as EditGridCellData;
List<MDGHelper.Row> list = new List<MDGHelper.Row>();
var mapping = MDGHelper.Mapping[GlobalVariable.IAML_MDGDiagram];
for (int i = 0; i < mapping.TaggedValueTypes.RefData.DataSet.Rows.Count; i++)
{
list.Add(mapping.TaggedValueTypes.RefData.DataSet.Rows[i]);
}
Dictionary<string, string> dicType = new Dictionary<string, string>();
Dictionary<string, string> dicData = new Dictionary<string, string>();
dicType.Clear();
dicData.Clear();
if (item != null && item is EditGridCellData)
{
TaggedValueModel taggedValueModel = cellDatas.RowData.Row as TaggedValueModel;
for (int i = 0; i < list.Count; i++)
{
dicType.Add(list[i].Columns[0].value, list[i].Columns[1].value);
}
for (int i = 0; i < list.Count; i++)
{
dicData.Add(list[i].Columns[0].value, list[i].Columns[2].value);
}
if (dicType.ContainsKey(taggedValueModel.Property) && dicType[taggedValueModel.Property] == "Enum")
{
int indexId = dicData[taggedValueModel.Property].LastIndexOfAny(new char[] { '=' });//找到最后一个等号的索引号
string strProperty = dicData[taggedValueModel.Property].Substring(indexId + 1);
string[] listPropertyValue = strProperty.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
taggedValueModel.ValueList = listPropertyValue.ToList();//获取数据
//实例化下拉列表框控件
FrameworkElementFactory comboBoxEdit = new FrameworkElementFactory(typeof(ComboBoxEdit));
comboBoxEdit.SetBinding(ComboBoxEdit.ItemsSourceProperty, new Binding()
{
Path = new PropertyPath("RowData.Row.ValueList"),
Mode = BindingMode.OneTime,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
comboBoxEdit.SetValue(ComboBoxEdit.MarginProperty, new Thickness(0));
comboBoxEdit.SetValue(ComboBoxEdit.BorderThicknessProperty, new Thickness(0));
comboBoxEdit.SetValue(ComboBoxEdit.EditValueProperty, "UNDEFINED");
comboBoxEdit.SetValue(ComboBoxEdit.IsTextEditableProperty, false);
dt.VisualTree = comboBoxEdit;
}
else if (dicType.ContainsKey(taggedValueModel.Property))
{
int DH_indexId = dicData[taggedValueModel.Property].LastIndexOfAny(new char[] { '=' });//找到最后一个等号的索引号
int FH_indexId = dicData[taggedValueModel.Property].LastIndexOfAny(new char[] { ';' });//找到最后一个分号的索引号
string strProperty = dicData[taggedValueModel.Property].Substring(DH_indexId + 1, FH_indexId - DH_indexId - 1);
if (dicType[taggedValueModel.Property] == "Decimal")
{
try
{
taggedValueModel.Value = Convert.ToDecimal(strProperty).ToString("N4");
}
catch (Exception)
{
taggedValueModel.Value = "";
}
}
else if (dicType[taggedValueModel.Property] == "Const")
taggedValueModel.Value = strProperty;
//else
// taggedValueModel.Value = "";
////实例化文本控件
FrameworkElementFactory txtBox = new FrameworkElementFactory(typeof(TextBox));
txtBox.SetBinding(TextBox.TextProperty, new Binding()
{
Path = new PropertyPath("RowData.Row.Value"),
Mode = BindingMode.OneTime,
UpdateSourceTrigger = UpdateSourceTrigger.Default
});
txtBox.SetValue(TextBox.ForegroundProperty, Brushes.Black);
txtBox.SetValue(TextBox.BackgroundProperty, new SolidColorBrush(Colors.Transparent));
txtBox.SetValue(TextBox.BorderThicknessProperty, new Thickness(0));
dt.VisualTree = txtBox;
}
else
{
//实例化文本控件
FrameworkElementFactory txtBox = new FrameworkElementFactory(typeof(TextBox));
txtBox.SetBinding(TextBox.TextProperty, new Binding()
{
Path = new PropertyPath("RowData.Row.Value"),
Mode = BindingMode.OneTime,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
txtBox.SetValue(TextBox.ForegroundProperty, Brushes.Black);
txtBox.SetValue(TextBox.BackgroundProperty, new SolidColorBrush(Colors.Transparent));
txtBox.SetValue(TextBox.BorderThicknessProperty, new Thickness(0));
dt.VisualTree = txtBox;
}
}
return dt;
}
}

posted @ 2020-02-22 10:58  honeys  阅读(307)  评论(0编辑  收藏  举报