I have a DataGrid
.
Inside it contains some columns; 2 are related to this question, one is a DataGridTextColumn
(x:Name="varTypeColumn"
) which show Variable type, another is a DataGridTemplateColumn
(x:Name="varValueColumn"
) which could be a TextBox or ComboBox inside.
If varTypeColumn
is Bool type, varValueColumn
should show a ComboBox
which contains 2 items: True
, False
. If varTypeColumn
is Int
Type, varValueColumn
should show a TextBox which let user input string.
So my question is that, is it possible to do it in xaml
? I find some implementation which do it in .cs
code, it try to get Row and get the Cell, at last set TextBox/ComboBox instance to Cell's Content property. It can work, but if the DataGrid contains big number items(e.g., more than 5000), it is very very slow to display it.
below is the code part:
private void InitEditors()
{
for (int i = 0; i < _devLinkCollectionView.Count; i++)
{
DataGridRow row = devLinkDataGrid.GetRow(i);
InitEditor(row);
}
}
private void InitEditor(DataGridRow row)
{
DevLink link = row.Item as DevLink;
if (link != null)
{
if (link.HasErrors)
{
ToolTipService.SetShowOnDisabled(row, true);
row.IsEnabled = false;
return;
}
// Create binding first
Binding binding = new Binding("DefaultValue")
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus,
Source = link
};
DataGridCell cell = devLinkDataGrid.GetCell(row, 4);
switch (link.VariableType)
{
case CarelStandardDataType.Bool:
ComboBox comboBox = new ComboBox();
comboBox.ItemsSource = new[] {string.Empty, "TRUE", "FALSE" };
comboBox.SetBinding(ComboBox.SelectedValueProperty, binding);
cell.Content = comboBox;
break;
default:
TextBox textBox = new TextBox();
textBox.Style = (Style)FindResource("TextBoxInError");
cell.Content = textBox;
binding.ValidationRules.Add(new DevLinkValidationRule(link));
textBox.SetBinding(TextBox.TextProperty, binding);
break;
}
}
}
maybe this is what you want.
==================sample data===============================