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

Binding to CurrentRecord

4 Answers 170 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 11 Aug 2008, 03:59 PM
Hello,

I'm facing some problems with CurrentRecord. I'm binding the object with CurrentRecord. I can't get the currentrecord from the code where I keep the object.

XAML

<UserControl x:Class="DevExpTest.PersonsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:model="clr-namespace:DevExpTest" 
    model:ObjectReference.Declaration="{model:ObjectReference window}"
    Height="419" Width="537" xmlns:my="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input">
    <UserControl.Resources>
        <ObjectDataProvider ObjectType="{x:Type model:PersonsViewModel}" x:Key="viewModel">
            <ObjectDataProvider.ConstructorParameters>               
                <model:ObjectReference Key="window"/>
            </ObjectDataProvider.ConstructorParameters>
        </ObjectDataProvider>
    </UserControl.Resources>
    <Grid>
        <my1:RadGridView Margin="0,10,28,43" Name="radGridView1"
                         xmlns:my1="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
                         ItemsSource="{Binding Source={StaticResource viewModel}, Path= GetData}"
                         CurrentRecord="{Binding Source={StaticResource viewModel}, Path= ActiveRecord}"
                         />
    </Grid>
</UserControl>

ViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;

namespace DevExpTest {
    public class PersonsViewModel : INotifyPropertyChanged {
        private IView _iView;
        private ListCollectionView _persons;
        private Person _activeRecord;

        public PersonsViewModel() {
            loadData();
        }
        private void loadData(){
            List<Person> p = new List<Person>();
            p.Add(new Person(){ ID = 1, Name= "Michael Sync" });
            p.Add(new Person(){ ID = 1, Name= "Julia" });

            _persons = new ListCollectionView(p);

        }
        public PersonsViewModel(IView iv)
            : this() {
            _iView = iv;
        }
        public Person ActiveRecord {
            get {
                return _activeRecord;
            }
            set {
                _activeRecord = value;
                SendPropertyChanged("ActiveRecord");
            }
        }
        public ListCollectionView GetData {
            get {
                //_persons.Filter = obj => {
                //    return false;
                //};

                return _persons;
            }
        }

        #region Event Handlers

        /// <summary>
        /// This method raises the property changed event.
        /// </summary>
        /// <param name="propertyName">The property name which value has been changed</param>
        protected void SendPropertyChanged(string propertyName) {
            if (_propertyChanged != null) {
                _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler _propertyChanged;

        public event PropertyChangedEventHandler PropertyChanged {
            add {
                _propertyChanged += value;
            }
            remove {
                _propertyChanged -= value;
            }
        }

        #endregion
    }
    public class Person : INotifyPropertyChanged {
        private int Id;
        private string name;

public string Name
{
  get { return name; }
  set { name = value;
      SendPropertyChanged("Name");
  }
}
        public int ID
        {
          get { return Id; }
          set { Id = value;
          SendPropertyChanged("ID");}
        }

        #region Event Handlers

        /// <summary>
        /// This method raises the property changed event.
        /// </summary>
        /// <param name="propertyName">The property name which value has been changed</param>
        protected void SendPropertyChanged(string propertyName) {
            if (_propertyChanged != null) {
                _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler _propertyChanged;

        public event PropertyChangedEventHandler PropertyChanged {
            add {
                _propertyChanged += value;
            }
            remove {
                _propertyChanged -= value;
            }
        }

        #endregion
    }
}


Error: Put the breakpoint in setter of ActiveRecord in ViewModel. That property will never get called.

You can download the test project in the comment of this post..

http://michaelsync.net/2008/08/09/any-recommendation-for-wpf-datagrid

Note: I think that there is a wrapper for data object in your datagrid. so, I think it might be the reason why it's not working. maybe, I think I need to call DataWrapper.ActiveRecord or something...

PS: Thanks a lot for visiting my blog,  Hristo!

4 Answers, 1 is accepted

Sort by
0
Chris
Telerik team
answered on 12 Aug 2008, 03:42 PM
Hi Michael,

Yes, you're right CurrentRecord is of type Telerik.Windows.Data.DataRecord which serves as a wrapper for the original data record. The original record could be accessed through the DataRecord.Data property.
One possible workaround for this particular scenario is to use a converter which could convert the value from DataRecord to the original data record type and vice versa. We could consider adding such converter either in the SP or in the next official release.

Another workaround is to declare the binding in the code rather than in xaml. I'm attaching the modified project. Btw, to make the binding possible I had to make the ActiveRecord property a dependency property.
I hope this helps.

Should you have any additional questions or problems, please do not hesitate to contact us again.

All the best,
Chris
the Telerik team

P.S. We managed to fix the other problem you've experienced (ListCollectionView with a filter which returns false for all records) and the fix will go in the SP (scheduler for the end of August). If this problem is a showstopper for you, just drop us a line and we'll send you a private build by the end of the week.

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Michael
Top achievements
Rank 1
answered on 12 Aug 2008, 05:14 PM
Thanks a lot for your reply..

>>Another workaround is to declare the binding in the code rather than in xaml

According to our team architecture, we try not to write anything in code-behinded file. All operations that we do are in either XAML or ViewModel.

So, I think other developers are not so happy to make our ViewModel to inherit from DependencyObject and write a few codes in code-behinded file of View.

Another thing that I noticed is that I need to click twice to select new row when I was running your modified sample.

Anyway, I'm very interested to look at Converter that you suggested.  Can I do something like that?

XAML

CurrentRecord="{Binding Source={StaticResource viewModel}, Converter={StaticResource myConverter}, ConverterParameter=DataRecord.Data, Path=ActiveRecord}"

Converter

public class BusinessUnitConverter : IValueConverter {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
           /// convert parameter to entity (e.g Person) and return it back??
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            throw new NotImplementedException();
        }

        #endregion
    }


>>>
If this problem is a showstopper for you, just drop us a line and we'll send you a private build by the end of the week.

Thanks. I would like to get it. Thanks.
0
Michael
Top achievements
Rank 1
answered on 12 Aug 2008, 05:29 PM
I would like to ask your opinion..

As I mentioned in my previous message, I don't like to write that code in the code-behinded file. I'm thinking to handle everything in XAML or ViewModel. Do you think it's okay for us to use your Datagrid for that scenario?
0
Atanas
Telerik team
answered on 13 Aug 2008, 04:07 PM
Hi Michael,

We tried this with a converter but the problem is that at the moment we use an internal binding to make sure that CurrentRecord and the last selected row are in sync. That's why currenty a two way binding (using a converter) to CurrentRecord won't work as we initially thought. We've already made some experiements and for the SP we could try to disconnect the current record from the selection which will make such bindings possible.
I'm sorry for the inconvenience. If this is a showstopper for you, we could send you a private build as soon as we manage to fix this.

Best wishes,
Atanas
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Chris
Telerik team
Michael
Top achievements
Rank 1
Atanas
Telerik team
Share this question
or