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

3 posts, 1 answers
  1. Dario Concilio
    Dario Concilio avatar
    129 posts
    Member since:
    Apr 2016

    Posted 12 Sep Link to this post

    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. Answer
    Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 13 Sep Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Dario Concilio
    Dario Concilio avatar
    129 posts
    Member since:
    Apr 2016

    Posted 14 Sep in reply to Dilyan Traykov Link to this post

    Thank you Dilyan perfect!
Back to Top