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

How to nested GridView into User Control and pass Dep. Properties

2 Answers 791 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dario Concilio
Top achievements
Rank 2
Dario Concilio asked on 12 Sep 2016, 12:01 PM

Hi,

I would create a UserControl called ListUserControl, it should contains RadgridView and other telerik controls. Ok.

But, what is the best way to wrap dependecy properties of GridView?

In primis: ItemSource, I created a dependency property in my user control, but how can I link this property to ItemSource of gridview, what is the best way to do this?

 

public partial class ListViewUserControl : UserControl
    {
        public ListViewUserControl()
        {
            InitializeComponent();
        }
 
        public object ItemSource
        {
            get { return (object) GetValue(ItemSourceProperty); }
            set { SetValue(ItemSourceProperty, value); }
        }
 
        // Using a DependencyProperty as the backing store for ItemSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemSourceProperty =
            DependencyProperty.Register(name: "ItemSource", propertyType: typeof(object), ownerType: typeof(object), typeMetadata: new PropertyMetadata(0));
 
 
    }

2 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 13 Sep 2016, 01:34 PM
Hello Dario,

In order to achieve the desired behavior, you will need to make the following modifications to your code:

1) Inside ListViewUserControl.xaml:

<Grid>
    <telerik:RadGridView ItemsSource="{Binding ItemSource, RelativeSource={RelativeSource AncestorType=my:ListViewUserControl}}" />
</Grid>

2) Inside ListViewUserControl.xaml.cs:

public partial class ListViewUserControl : UserControl
{
    public ListViewUserControl()
    {
        InitializeComponent();
    }
 
    public object ItemSource
    {
        get { return (object)GetValue(ItemSourceProperty); }
        set { SetValue(ItemSourceProperty, value); }
    }
     
    public static readonly DependencyProperty ItemSourceProperty =
        DependencyProperty.Register(name: "ItemSource", propertyType: typeof(object), ownerType: typeof(ListViewUserControl), typeMetadata: new PropertyMetadata(null));
}

You may also want to have a look at the following resources:
A Simple Pattern for Creating Reusable UserControls
Try it: Create a custom WPF control
Custom Control Dependency Property Binding

I hope you find this helpful. Please let me know if you need any further assistance on the matter.

Regards,
Dilyan Traykov
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Dario Concilio
Top achievements
Rank 2
answered on 14 Sep 2016, 09:10 AM
Thank you Dilyan perfect!
Tags
GridView
Asked by
Dario Concilio
Top achievements
Rank 2
Answers by
Dilyan Traykov
Telerik team
Dario Concilio
Top achievements
Rank 2
Share this question
or