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

Binding GridViewDataColumn.Header to model property fails

1 Answer 602 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 22 Jun 2015, 03:52 PM

Hi,

I'm really pulling my hair with this one, so any help I can get would be greatly appreciated!

Here is the thing... I am using MVVM and I need to create a grid with (to start with) one row and a variable number of columns. The name of the columns are based on property values in an object (see "ProductVariantFacade" below), so I need to set the name of the columns after the grid has been initialized. So I started out using an ObservableCollection<ProductVariantFacade> in my view model, like so:

public ObservableCollection<ProductVariantFacade> ProductVariantFacades { get; set; }

Where ProductVariantFacade looks like this:

public class ProductVariantFacade : FacadeBase
    {
        public ProductVariantFacade()
        {
            Data = new List<ProductVariantData>();
        }
 
        private string rowName;
        public string RowName
        {
            get { return rowName; }
            set
            {
                if (value != rowName)
                {
                    rowName = value;
                    OnPropertyChanged("RowName");
                }
            }
        }
 
        public List<ProductVariantData> Data { get; set; }
 
        public void AddData(string headerName, string value)
        {
            Data.Add(new ProductVariantData { HeaderName = headerName, Value = value });
        }
    }
 
    public class ProductVariantData : FacadeBase
    {
        private string value;
        public string Value
        {
            get { return this.value; }
            set
            {
                if (value != this.value)
                {
                    this.value = value;
                    OnPropertyChanged("Value");
                }
            }
        }
 
        private string headerName;
        public string HeaderName
        {
            get { return headerName; }
            set
            {
                if (value != headerName)
                {
                    headerName = value;
                    OnPropertyChanged("HeaderName");
                }
            }
        }
    }

The inherited FacadeBase implements INotifyPropertyChanged. Then my RadGridView's columns are set up as so:

<telerik:RadGridView Name="VariantDetailsGrid"
                                 AutoGenerateColumns="False"
                                 Height="130"
                                 IsReadOnly="False"
                                 ItemsSource="{Binding ProductVariantFacades, Mode=TwoWay}"
                                 >
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding RowName}" Header="" DataType="{x:Type sys:String}"/>
                     
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Data[0].Value}" DataType="{x:Type sys:String}">
                        <telerik:GridViewDataColumn.Header>
                            <TextBlock Text="{Binding ProductVariantFacades[0].Data[0].HeaderName, Mode=TwoWay, Source={StaticResource ProductViewModel}}" />
                        </telerik:GridViewDataColumn.Header>
                    </telerik:GridViewDataColumn>
<telerik:RadGridView.Columns>
</telerik:RadGridView>

I was hoping that when I set my ObservableCollection ProductVariantFacades in my view model, the name of the column (the header) would be updated as well. That is, when the ProductVariantFacades[0].Data[0].HeaderName is set to a value. But for some reason this doesn't work. If I via a Button change the value of e.g. ProductVariantFacades[0].Data[0].Value it gets updated in the grid. But the header does not get updated when I change it. Any idea as to why?

Could it have anything to do with that the DataContext of TextBlock is not the same as for the rest of the view? E.g. not the same as the DataContext that the Data[0].Value is "connected" to?

Please help, thanks!

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 25 Jun 2015, 10:35 AM
Hello Andy,

Indeed, the DataContext of the column headers needs to be set explicitly. This can be achieved by pointing to an element by using ElementName, or via Source. Since you have used the view model as a DataContext, can you please share its implementation?

I am also attaching a sample project in which binding the TextBlock within the header of GridViewDataColumn is working. Can you please take a look at it and let me know how it differs from your project setup?

Best Regards,
Stefan X1
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Andy
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or