This is a migrated thread and some comments may be shown as answers.

RadGridView ColumnsCollection

5 Answers 70 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andreas
Top achievements
Rank 1
Andreas asked on 19 Oct 2012, 10:05 AM
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:

<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

5 Answers, 1 is accepted

Sort by
0
Accepted
Dimitrina
Telerik team
answered on 19 Oct 2012, 01:11 PM
Hello,

You can check this demo about how we are Binding the Columns using an attached behaviour. You can also check your local copy of our Demos and especially MyColumnsBindingBehavior class. 

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Andreas
Top achievements
Rank 1
answered on 23 Oct 2012, 03:45 PM
Hello,

I'm afraid to get the same problem with your attached behavior, please take a look to the referenced errors in the attachments. I hope somebody can help me.

Regards,
Andreas
0
Andreas
Top achievements
Rank 1
answered on 23 Oct 2012, 04:02 PM
Sorry for the incomplete class view, there is a image attached with the full view, the code in img4 is the same as on the unchanged Telerik's MyColumnsBindingBehavior.cs, see in img5.
0
Dimitrina
Telerik team
answered on 24 Oct 2012, 10:50 AM
Hello,

This is indeed strange as the same behaviour works fine on our demo. You can check your local copy of the Demos.

You have the complete source code available within the Demos folder available with RadControls.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Andreas
Top achievements
Rank 1
answered on 06 Nov 2012, 10:09 AM
Some day later i found out, that the ColumnsCollection only apply after i opening the window the second time, i have changed the GridViewColumn to the GridViewDataColumn and i applied IsAutoGenerated to false, so now its working like it should, it applies the ColumnsCollection at the first time.

<custom:CustomRadGridView ColumnsCollection="{Binding ColumnsCollection}" ... />

ColumnsCollection.Add(new GridViewDataColumn() { ..., IsAutoGenerated = false, CellTemplate = (DataTemplate)XamlReader.Load(CellETemp.ToString()) });

Tags
GridView
Asked by
Andreas
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Andreas
Top achievements
Rank 1
Share this question
or