Welcome to the Xceed Community | Help
Community Search  
More Search Options

How to bind 2 columns to same property

Sort Posts: Previous Next
  •  01-07-2009, 9:08 PM Post no. 17739

    How to bind 2 columns to same property

    I need to display a datagrid with two columns that are bound to the same property. Here is the code I am using but it does not work:

                 DataGridCollectionView dataGridCollectionView = new DataGridCollectionView(list, typeof(MyData), false, false);

                DataGridItemProperty datagridItem = new DataGridItemProperty();
                datagridItem.Name = "Str";
                datagridItem.Title = "My Data 1";
                datagridItem.DataType = typeof(string);
                datagridItem.IsReadOnly = false;
                dataGridCollectionView.ItemProperties.Add(datagridItem);

                datagridItem = new DataGridItemProperty();
                datagridItem.Name = "Str";
                datagridItem.Title = "My Data 2";
                datagridItem.DataType = typeof(string);
                datagridItem.IsReadOnly = false;
                dataGridCollectionView.ItemProperties.Add(datagridItem);

                grid.ItemsSource = dataGridCollectionView;

    This does not work as the Name properties on the DataGridItemProperty objects are the same. Is there another way to bind both columns to the same property?

     Thanks.

  •  01-08-2009, 2:33 AM Post no. 17741 in reply to 17739

    Re: How to bind 2 columns to same property

    The key is that you can use the ValuePath of the DataGridItemProperty as shown in this thread http://xceed.com/CS/forums/thread/14339.aspx .

    I believe that if the ValuePath is not provided then the grid will internally use the Name - but the Name has to be unique so when you do use ValuePath you will have to change the name field to be unique for each column.

  •  01-08-2009, 1:50 PM Post no. 17755 in reply to 17741

    Re: How to bind 2 columns to same property

    We initially tried that but ran into a problem, where if the data source is modified then the grid cell is not updated until you explicitly call refresh. Here is the updated code which shows the problem.

                ObservableCollection<MyData> list = new ObservableCollection<MyData>();

                list.Add(new MyData("Test 1"));
                list.Add(new MyData("Test 2"));
                list.Add(new MyData("Test 3"));

                DataGridCollectionView dataGridCollectionView = new DataGridCollectionView(list, typeof(MyData), false, false);

                DataGridItemProperty datagridItem = new DataGridItemProperty();
                datagridItem.Name = "Str";
                datagridItem.Title = "My Data 1";
                datagridItem.DataType = typeof(string);
                dataGridCollectionView.ItemProperties.Add(datagridItem);

                datagridItem = new DataGridItemProperty();
                datagridItem.Name = "Str2";
                datagridItem.ValuePath = "Str";
                datagridItem.Title = "My Data 2";
                datagridItem.DataType = typeof(string);
                dataGridCollectionView.ItemProperties.Add(datagridItem);

                grid.ItemsSource = dataGridCollectionView;

     

     Our data class, MyData, implements INotifyPropertyChanged and looks like this:

    public class MyData : INotifyPropertyChanged
        {
            string s;

            public string Str
            {
                get { return s; }
                set
                {
                    if (s != value)
                    {
                        s = value;
                        NotifyPropertyChanged("Str");
                    }
                }
            }

            public MyData(string _s)
            {
                s = _s;
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
        }

     

    Now both columns should bind to the single property named "Str". Let's say the data source changes (i.e. the value of Str property changes due to some other user action), then the first column cell is updated immediately, but the second column cell is not updated until you call Refresh on the grid items. Seems like using ValuePath of DataGridItemProperty causes some problems with binding. How can we resolve that?

    Thanks.

     

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.