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

How to define a dependency property on a custom column and use the row item as datacontext

5 Answers 457 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Licenses
Top achievements
Rank 1
Licenses asked on 13 Dec 2013, 03:56 PM
Hello,

I have a grid bound to a collection of items.

An item has several properties and one property is a combination of 4 properties (omitted INotifyPropertyChanged etc):
public class MyItemViewModel
{
    public int Number { get; set; }
    public string NumberSeperator { get; set; }
    public int SubNumber { get; set; }
    public string SubSubNumber { get; set; }
     
    public string FullNumber {
        get
        {
            return string.Format("{0}{1}{2}{3}", Number, NumberSeperator, SubNumber, SubSubNumber);
        }
    }
}


There are of course more properties available but the focus is on these properties mentioned.
Now, I tried creating a custom column which will, in view mode, display the FullNumber property and in edit mode create 4 masked text boxes to edit those 4 properties seperatly (Number, NumberSeperator, SubNumber and SubSubNumber)

So I figured the cleanest & easiest way to do this was to define 5 dependency properties on the custom column, 1 for each property, so that I could bind to them in my xaml code:

<MyCustomColumn NumberBinding="{Binding Number}" NumberSeperatorBinding="{Binding NumberSeperator}" SubNumberBinding="{Binding SubNumber}" SubSubNumberBinding="{Binding SubSubNumber}" />


So my column looked a bit like this:

public class MyCustomColumn : GridViewBoundColumnBase
{
    public Binding NumberBinding
    {
        get { return (Binding)GetValue(NumberBindingProperty); }
        set { SetValue(NumberBindingProperty, value); }
    }
    public static readonly DependencyProperty NumberBindingProperty =
        DependencyProperty.Register("NumberBinding", typeof(Binding), typeof(MyCustomColumn), new PropertyMetadata(null));
         
    public Binding NumberSeperatorBinding
    {
        get { return (Binding)GetValue(NumberSeperatorBindingProperty); }
        set { SetValue(NumberSeperatorBindingProperty, value); }
    }
    public static readonly DependencyProperty NumberSeperatorBindingProperty =
        DependencyProperty.Register("NumberSeperatorBinding", typeof(Binding), typeof(MyCustomColumn), new PropertyMetadata(null));
         
    public Binding SubNumberBinding
    {
        get { return (Binding)GetValue(SubNumberBindingProperty); }
        set { SetValue(SubNumberBindingProperty, value); }
    }
    public static readonly DependencyProperty SubNumberBindingProperty =
        DependencyProperty.Register("SubNumberBinding", typeof(Binding), typeof(MyCustomColumn), new PropertyMetadata(null));
         
    public Binding SubSubNumberBinding
    {
        get { return (Binding)GetValue(SubSubNumberBindingProperty); }
        set { SetValue(SubSubNumberBindingProperty, value); }
    }
    public static readonly DependencyProperty SubSubNumberBindingProperty =
        DependencyProperty.Register("SubSubNumberBinding", typeof(Binding), typeof(MyCustomColumn), new PropertyMetadata(null));
}


So when overriding the CreateCellEditElement function, I implemented is as such:

public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
 {
    var e1 = new RadMaskedNumericInput();
    e1.SetBinding(RadMaskedNumericInput.ValueProperty, NumberBinding);
     
    var e2 = new RadMaskedTextInput();
    e2.SetBinding(RadMaskedTextInput.ValueProperty, NumberSeperatorBinding);
     
    var e3 = new RadMaskedNumericInput();
    e3.SetBinding(RadMaskedNumericInput.ValueProperty, SubNumberBinding);
     
    var e4 = new RadMaskedTextInput();
    e4.SetBinding(RadMaskedTextInput.ValueProperty, SubSubNumberBinding);
     
    var wp = new WrapPanel();
     
    wp.Children.Add(e1);
    wp.Children.Add(e2);
    wp.Children.Add(e3);
    wp.Children.Add(e4);
     
    return wp;
 }


However, when using this column it will complain that it can't find the Number, NumberSeperator, SubNumber and SubSubNumber properties on the viewmodel, which is the datacontext of the grid.

I thought it would work the same as the DataMemberBinding and automatically use as datacontext, the row item.

Any help would be appreciated :)

5 Answers, 1 is accepted

Sort by
0
Licenses
Top achievements
Rank 1
answered on 17 Dec 2013, 08:28 AM
Any ideas?
0
Dimitrina
Telerik team
answered on 18 Dec 2013, 04:03 PM
Hello,

You just set the DataContext for RadGridView. 

In order to have the DataContext of each GridViewRow to be an object from the bound collection, you need to have set an ItemsSource collection for RadGridView.

To get the Binding still be resolved in your case, you could specify a valid Source for it. You can define the MyItemViewModel as a StaticResource and then set it as a Source.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Pablo
Top achievements
Rank 1
answered on 22 Jun 2016, 11:47 AM

Sorry,

I have the same problem. Did you find a solution?

0
Stefan
Telerik team
answered on 23 Jun 2016, 11:08 AM
Hi Pablo,

Can you please check out the Create Custom Column Editor and Create Custom Editor topics, as I believe you will find them helpful on this matter?

Hope this helps.

Best Regards,
Stefan X1
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Sdfg
Top achievements
Rank 1
answered on 26 Jan 2017, 12:59 PM

Did anyone solve this? I have the same problem.

Regarding suggested solutions;

Dimitrina: The question about datacontext is for the context of the binding in the custom column, not for the gridview.

Stefan X1: Those topics doesn't cover this scenario creating a new bindable property for the column that should bind on the child elements like the datamemberbinding does.

 

Tags
GridView
Asked by
Licenses
Top achievements
Rank 1
Answers by
Licenses
Top achievements
Rank 1
Dimitrina
Telerik team
Pablo
Top achievements
Rank 1
Stefan
Telerik team
Sdfg
Top achievements
Rank 1
Share this question
or