Dear Experts,
I have a RadGridView which is programmatically created. In a column I have a RadComboBox.
The ItemsSource of the ComboBox is dynamic per line. So it's content will change per line depending on another column's field value. So it is NOT a autocomplete functionality in which the dropdown is created while you type. It is created while you open the list.
At the moment, I am using the DropDownOpened to assign a new ItemsSource to the ComboBox.
Problem:
The dropdown shows fine, but the values are not written back to the column field once you select an entry and the combo is unfocused. See screenshots: dynamic-??.PNG.
I have tried to use different BindingModes (has to be programmatically) - unfortunately without success.
Thanks for your feedback.
Best Regards,
Phil
Here is the code:
<
UserControl.Resources
>
<
DataTemplate
x:Key
=
"cellEditTemplateDrp"
>
<
telerik:RadComboBox
Name
=
"cmbBox"
DropDownOpened
=
"cmbBox_DropDownOpened"
>
<
telerik:RadComboBox.ItemTemplate
>
<
DataTemplate
>
<
StackPanel
Orientation
=
"Horizontal"
>
<
TextBlock
Text
=
"{Binding ID}"
Margin
=
"0,0,5,0"
Width
=
"30"
/>
<
TextBlock
Text
=
"{Binding Description}"
/>
</
StackPanel
>
</
DataTemplate
>
</
telerik:RadComboBox.ItemTemplate
>
</
telerik:RadComboBox
>
</
DataTemplate
>
</
UserControl.Resources
>
<
Grid
x:Name
=
"LayoutRoot"
Loaded
=
"LayoutRoot_Loaded"
>
<
telerik:RadGridView
HorizontalAlignment
=
"Left"
Name
=
"radGridView2"
VerticalAlignment
=
"Top"
AutoGenerateColumns
=
"False"
/>
</
Grid
>
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
// get this data dynamically from an external source (not a database)
DataTable table = new DataTable();
table.Columns.Add(new DataColumn()
{ ColumnName = "ID", DataType = typeof(int) });
table.Columns.Add(new DataColumn()
{ ColumnName = "Name", DataType = typeof(string) });
table.Columns.Add(new DataColumn()
{ ColumnName = "InputString1", DataType = typeof(string) });
table.Columns.Add(new DataColumn()
{ ColumnName = "InputString2", DataType = typeof(string) });
table.Columns.Add(new DataColumn()
{ ColumnName = "InputString3", DataType = typeof(string) });
for (var i = 0; i < 1000; i++)
{
DataRow row = new DataRow();
row["ID"] = i;
row["Name"] = "Name " + i.ToString();
row["InputString1"] = i.ToString();
row["InputString2"] = i.ToString();
row["InputString3"] = "1";
table.Rows.Add(row);
}
// generate columns
this.GenerateDetailColumns(table);
// assign source
this.radGridView2.ItemsSource = table;
}
private void GenerateDetailColumns(DataTable list)
{
this.radGridView2.Columns.Clear();
foreach (DataColumn column in list.Columns)
{
GridViewDataColumn col = new GridViewDataColumn();
col.UniqueName = column.ColumnName;
col.Header = column.ColumnName;
if(column.ColumnName == "InputString3") // dynamically set combo
{
col.CellEditTemplate = Resources["cellEditTemplateDrp"] as DataTemplate;
}
this.radGridView2.Columns.Add(col);
}
}
private void cmbBox_DropDownOpened(object sender, EventArgs e)
{
RadComboBox cmb = sender as RadComboBox;
DataTable table = new DataTable();
table.Columns.Add(new DataColumn()
{ ColumnName = "ID", DataType = typeof(string) });
table.Columns.Add(new DataColumn()
{ ColumnName = "Description", DataType = typeof(string) });
for (var i = 0; i < 10; i++)
{
DataRow row = new DataRow();
row["ID"] = i.ToString();
row["Description"] = "Description " + i.ToString();
table.Rows.Add(row);
}
cmb.ItemsSource = table.ToList();
/*
// this binding does not resolve the problem. it ends up with an empty dropdownlist
System.Windows.Data.Binding bind = new System.Windows.Data.Binding();
bind.Mode = System.Windows.Data.BindingMode.OneWay;
cmb.SetBinding(RadComboBox.ItemsSourceProperty, bind);
System.Windows.Data.Binding bind2 = new System.Windows.Data.Binding("ID");
bind2.Mode = System.Windows.Data.BindingMode.TwoWay;
cmb.SetBinding(RadComboBox.SelectedValueProperty, bind);
*/
}
}