Hi,
in MVVM scenario I need dynamically generate columns for RadGridView.
I created attached property for this purpose.
ItemSource is bind to property ObservableCollection<Company> Companies from VM
where Company is
and User is
I generate columns in VM in this way.
In VM I have property ObservableCollection<ColumnDefinition> which is bind to attached property in View.
ColumnDefinition is abstraction for GridViewDataColumn.
When I need add column to RadGridView I add ColumnDefinition obj to property Columns.
I need apply DataTemplate on GridViewDataColumn and this is which not work for me.
Exactly binding to property User.LastName in DataTemplate not work.
I use this binding exp for GridViewDataColumn
I try attached several times sample project but always get error:
The selected file(s) cannot be attached because it may exceed the maximum attachment size (2 MB) or is from not allowed type (allowed: .gif, .jpg, .jpeg, .png).
Attached file size is 74 KB.
Hence I uploded sample project to onedrive.
You can download here.
in MVVM scenario I need dynamically generate columns for RadGridView.
I created attached property for this purpose.
ItemSource is bind to property ObservableCollection<Company> Companies from VM
where Company is
public
class
Company : INotifyPropertyChanged
{
public
Company()
{
Dept =
new
Dictionary<
string
, User>();
}
private
Dictionary<
string
,User> dept;
public
Dictionary<
string
,User> Dept
{
get
{
return
dept; }
set
{
dept = value;
OnPropertyChanged(
"Dept"
);
}
}
}
and User is
public
class
User : INotifyPropertyChanged
{
private
string
firstName;
public
string
FirstName
{
get
{
return
firstName; }
set
{
firstName = value;
OnPropertyChanged(
"FirstName"
);
}
}
private
string
lastName;
public
string
LastName
{
get
{
return
lastName; }
set
{
lastName = value;
OnPropertyChanged(
"LastName"
);
}
}
}
I generate columns in VM in this way.
In VM I have property ObservableCollection<ColumnDefinition> which is bind to attached property in View.
ColumnDefinition is abstraction for GridViewDataColumn.
<
telerik:RadGridView
ItemsSource
=
"{Binding Path=Country.Companies, UpdateSourceTrigger=PropertyChanged}"
dynamicCol:RadGridViewColumnsBinding.ColumnsCollection
=
"{Binding Path=Columns}"
>
<
telerik:RadGridView.Resources
>
<
DataTemplate
x:Key
=
"UserTemplate"
DataType
=
"{x:Type models:User}"
>
<
StackPanel
>
<
TextBlock
Text
=
"LastName:"
/>
<
TextBlock
Text
=
"{Binding Path=LastName}"
/>
</
StackPanel
>
</
DataTemplate
>
</
telerik:RadGridView.Resources
>
</
telerik:RadGridView
>
When I need add column to RadGridView I add ColumnDefinition obj to property Columns.
I need apply DataTemplate on GridViewDataColumn and this is which not work for me.
Exactly binding to property User.LastName in DataTemplate not work.
I use this binding exp for GridViewDataColumn
string
bindingExp =
string
.Format(
"Dept[{0}]"
, columnName);
//string bindingExp = string.Format("Dept[{0}].LastName", columnName); // whithout datatemplate this work
var binding =
new
Binding(bindingExp)
{
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
Columns.Add(
new
ColumnDefinition
{
Header = columnName,
DataMemberBinding = binding,
Tag = ColumnTag,
UniqueName = Guid.NewGuid().ToString(),
CellTemplateName =
"UserTemplate"
,
});
I try attached several times sample project but always get error:
The selected file(s) cannot be attached because it may exceed the maximum attachment size (2 MB) or is from not allowed type (allowed: .gif, .jpg, .jpeg, .png).
Attached file size is 74 KB.
Hence I uploded sample project to onedrive.
You can download here.