Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Oversight, or intended behavior?

$
0
0

Sometimes, when doing a binding, you don't want to indicate a subpath...you want the binding to be to the whole item. Then, in a converter class, you "build" the information you need to, accessing all fields necessary. For example (and this is code I'm making up for this post):

public class Foo
{
        DateTime _date;
        String _name;
        int _number;
}

Then you have a collection of them:

public class FooList : ObservableCollection<Foo>
{
        private static readonly FooList instance = new FooList();
        public static FooList Instance
        {
                get { return instance; }
        }
}

And you use this to populate a UI list:

ListView lv = new ListView();
lv.ItemsSource = FooList.Instance;

Now. You want to define a DataTemplate for the cells of that list. So, you first create a converter class:

public class FooToStringConverter : IValueConverter
{
        public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
                Foo f = value as Foo;
                if( f != null )
                {
                        StringBuilder sr = new StringBuilder();
                        sr.Append( f._date.Date.ToString( "MM/dd/yyyy" ) );
                        sr.Append( " : " );
                        sr.Append( f._number.ToString() );
                        sr.Append( ", " );
                        sr.Append( f._name );
                        return sr.ToString();
                }
                return String.Empty;
        }

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

Then you set up your DataTemplate:

TextCell cell = new DataTemplate( typeof( TextCell ) );
Binding b = new Binding();
b.Converter = new FooToStringConverter();
cell.SetBinding( TextCell.TextProperty, b );

Only, at runtime, this throws an exception that the Path property cannot be null. You can apparently do this null Path binding with some controls (Image), but not others (Cell). To get around this, I had to put a member on my class that simply returns the current instance. So, for this example (new code bracketed in asterisks):

public class Foo
{
        DateTime _date;
        String _name;
        int _number;
        **public Foo local { get { return this; } }**
}

...

TextCell cell = new DataTemplate( typeof( TextCell ) );
Binding b = new Binding();
b.Converter = new FooToStringConverter();
**b.Path = "local";**
cell.SetBinding( TextCell.TextProperty, b );

Now, obviously I could have added a "property" to Foo that would build and return the string, and bind to that, but in my actual usage, the class is far more complicated and has many more members than this example. Is this an oversight? If not, why was the decision made to not allow Cell bindings to use the entire object, instead of an individual field?


Viewing all articles
Browse latest Browse all 58056

Trending Articles