We have a lot of dynamic table to create columns(see attached file) basing on MVVM, We can not create codebehind in *.xaml.cs. This solution is ok to create genernal text for "GridViewDataColumn". But now we have a scenario to create dropdown list in cell. In this column, all itemsouce in dropdownlist are totally different. W
due to following reson, we could not and it is impossible to using this.
1. populate itemSource to columns in code behind
2. could not change current architecture.
we are using properity change event to dynamically create columns. additional, we only could assign binding in create columns. therefore, in many thread even in telerik sdk, we are using itemssourceBinding to do this. but it shows blank in cell when runtime.
Please refer to attached sample code. Is it possible to dynamically to create dropdown list basing on current design.
Important note : our version is 2012.1.215.1040
Add column code :
ViewModel:
ViewModelList
ExtendTreeList control
XAML
Thanks a lot
due to following reson, we could not and it is impossible to using this.
1. populate itemSource to columns in code behind
2. could not change current architecture.
we are using properity change event to dynamically create columns. additional, we only could assign binding in create columns. therefore, in many thread even in telerik sdk, we are using itemssourceBinding to do this. but it shows blank in cell when runtime.
Please refer to attached sample code. Is it possible to dynamically to create dropdown list basing on current design.
Important note : our version is 2012.1.215.1040
Add column code :
if
(field.Key ==
"1"
)
{
GridViewComboBoxColumn column =
new
GridViewComboBoxColumn();
column.Header = field.Value.Caption;
//data member binding
Binding dataMemberBinding =
new
Binding(String.Format(
"TemplateValues[{0}]"
, field.Key));
column.DataMemberBinding = dataMemberBinding;
//Items Source Binding
Binding itemBinding =
new
Binding(
string
.Format(
"TemplateDropDownValues[{0}]"
, field.Key));
column.ItemsSourceBinding = itemBinding;
column.DisplayMemberPath =
"Name"
;
column.SelectedValueMemberPath =
"ID"
;
control.Columns.Insert(currentIndex, column);
}
else
if
(field.Key ==
"2"
)
{
GridViewDataColumn column =
new
GridViewDataColumn();
column.Header = field.Value.Caption;
Binding bind =
new
Binding(String.Format(
"TemplateValues[{0}]"
, field.Key));
column.DataMemberBinding = bind;
control.Columns.Insert(currentIndex, column);
}
ViewModel:
public
FolderViewModel(
int
schoolCount=1)
{
Dictionary<
string
,
object
> values =
new
Dictionary<
string
,
object
>();
Dictionary<
string
,
object
> lists =
new
Dictionary<
string
,
object
>();
//It is for dropdown list
List<School> schools =
new
List<School>();
School s1 =
new
School
{
ID = 1,
schoolValue = 1,
Name =
"A-school"
};
if
(schoolCount==2)
{
School s2 =
new
School
{
Name =
"B-school"
,
schoolValue = 2,
ID = 2
};
schools.Add(s2);
}
schools.Add(s1);
lists.Add(
"1"
, schools);
TemplateDropDownValues = lists;
//here for grid view
values.Add(
"1"
, 0);
TemplateValues = values;
values.Add(
"2"
,
"This is in cell"
);
}
/// <summary>
/// Grid view items
/// </summary>
public
Dictionary<
string
,
object
> TemplateValues
{
get
;
set
;
}
/// <summary>
/// drop list items
/// </summary>
public
Dictionary<
string
,
object
> TemplateDropDownValues
{
get
;
set
;
}
ViewModelList
public
FolderListViewModel()
{
//add 2 rows
ObservableCollection<FolderViewModel> folders =
new
ObservableCollection<FolderViewModel>();
FolderViewModel f1 =
new
FolderViewModel(1);
FolderViewModel f2 =
new
FolderViewModel(2);
folders.Add(f1);
folders.Add(f2);
//add 2 columns
Dictionary<
string
, DataColumn> column =
new
Dictionary<
string
, DataColumn>();
column.Add(
"1"
,
new
DataColumn(
"DropDownList Column"
));
column.Add(
"2"
,
new
DataColumn(
"General Column"
));
TemplateNames = column;
Items = folders;
}
//binding items
public
ObservableCollection<FolderViewModel> Items {
get
;
set
; }
//columns
public
Dictionary<
string
, DataColumn> TemplateNames {
get
;
set
;}
ExtendTreeList control
public
class
ExtendedRadTreeListView : RadTreeListView
{
private
Dictionary<
string
, DataColumn> TemplateFields
{
get
;
set
;
}
public
Dictionary<
string
, DataColumn> DynamicColumns
{
get
{
return
(Dictionary<
string
, DataColumn>)GetValue(DynamicColumnsProperty);
}
set
{
SetValue(DynamicColumnsProperty, value);
}
}
public
static
readonly
DependencyProperty DynamicColumnsProperty = DependencyProperty
.Register(
"DynamicColumns"
,
typeof
(Dictionary<
string
, DataColumn>)
,
typeof
(ExtendedRadTreeListView),
new
PropertyMetadata(OnDynamicColumnsChanged));
private
static
void
OnDynamicColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ExtendedRadTreeListView grid = d
as
ExtendedRadTreeListView;
if
(e.NewValue !=
null
&& e.NewValue
is
Dictionary<
string
, DataColumn>)
{
grid.TemplateFields = (e.NewValue
as
Dictionary<
string
, DataColumn>);
AddDynamicColumns.AddColumns(grid, grid.TemplateFields);
}
}
}
XAML
<
UserControl
x:Class
=
"SilverlightApplication27.MainPage"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
xmlns:controls
=
"clr-namespace:SilverlightApplication27"
mc:Ignorable
=
"d"
xmlns:Behaviors
=
"clr-namespace:SilverlightApplication27;assembly=SilverlightApplication27"
d:DesignHeight
=
"300"
d:DesignWidth
=
"400"
>
<
Grid
x:Name
=
"LayoutRoot"
Loaded
=
"LayoutRoot_Loaded"
Background
=
"White"
>
<
controls:ExtendedRadTreeListView
x:Name
=
"RadTreeListView1"
AutoGenerateColumns
=
"False"
CanUserFreezeColumns
=
"False"
IsReadOnly
=
"True"
ItemsSource
=
"{Binding Items, Mode=TwoWay}"
DynamicColumns
=
"{Binding TemplateNames, Mode=TwoWay}"
>
<
i:Interaction.Behaviors
>
<
Behaviors:LoadBehavior
/>
</
i:Interaction.Behaviors
>
</
controls:ExtendedRadTreeListView
>
</
Grid
>
</
UserControl
>
Thanks a lot