This question is locked. New answers and comments are not allowed.
Hi there,
i have tried to use this solution, to bind dynamically columns to a RadGridView at runtime:
http://www.telerik.com/community/forums/silverlight/gridview/radgridview-columns-binding-mvvm.aspx
But attached properties never worked for me. So i have decided to create my own class inherit by RadGridView with a dependency property, the result looks so:
And the code for it is:
So at all it looks good, and it will compile without any warnings and errors. But the binding will never update the ColumnsCollection, it is notifying the window where the binding is defined, but it doesn't step into the dependency property,
I can't get a clear solution for this, but its really nessecary to dynamically bind the columns at runtime to the gridview.
I have provided a sample solution at this sites:
https://docs.google.com/open?id=0B2Gh-Fj36cJKdDFMRTQ4eGZyN2s
https://www.dropbox.com/s/rnj792gnz1x1kyr/CustomControlPropertyBinding.zip
i have tried to use this solution, to bind dynamically columns to a RadGridView at runtime:
http://www.telerik.com/community/forums/silverlight/gridview/radgridview-columns-binding-mvvm.aspx
But attached properties never worked for me. So i have decided to create my own class inherit by RadGridView with a dependency property, the result looks so:
<
local:CustomRadGridView
Grid.Row
=
"0"
CustomColumnsCollection
=
"{Binding ColumnsCollection}"
ItemsSource
=
"{Binding Data}"
/>
And the code for it is:
public
class
CustomRadGridView : RadGridView
{
public
CustomRadGridView()
{
//New Default Settings
base
.ScrollMode = ScrollUpdateMode.RealTime;
base
.DataLoadMode = Telerik.Windows.Controls.GridView.DataLoadMode.Asynchronous;
base
.IsReadOnly =
true
;
base
.AutoGenerateColumns =
false
;
base
.SelectionMode = System.Windows.Controls.SelectionMode.Single;
}
public
ObservableCollection<GridViewColumn> CustomColumnsCollection
{
get
{
return
(ObservableCollection<GridViewColumn>)GetValue(CustomColumnsCollectionProperty); }
set
{ SetValue(CustomColumnsCollectionProperty, value); }
}
public
static
readonly
DependencyProperty CustomColumnsCollectionProperty =
DependencyProperty.Register(
"CustomColumnsCollection"
,
typeof
(ObservableCollection<GridViewColumn>),
typeof
(CustomRadGridView),
new
FrameworkPropertyMetadata(
new
ObservableCollection<GridViewColumn>(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ColumnsCollectionsCallBack));
private
static
void
ColumnsCollectionsCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
GridViewDataControl grid = d
as
GridViewDataControl;
if
(grid !=
null
)
{
grid.Columns.Clear();
foreach
(GridViewColumn gc
in
(ObservableCollection<GridViewColumn>)e.NewValue)
grid.Columns.Add(gc);
}
}
}
So at all it looks good, and it will compile without any warnings and errors. But the binding will never update the ColumnsCollection, it is notifying the window where the binding is defined, but it doesn't step into the dependency property,
I can't get a clear solution for this, but its really nessecary to dynamically bind the columns at runtime to the gridview.
I have provided a sample solution at this sites:
https://docs.google.com/open?id=0B2Gh-Fj36cJKdDFMRTQ4eGZyN2s
https://www.dropbox.com/s/rnj792gnz1x1kyr/CustomControlPropertyBinding.zip