Forum Xamarin Xamarin.Forms

Access a binding content

KrizKriz DEMember ✭✭

Hi there!

I know how to bind e.g. a label.textproperty to a bindingcontext. But how can i get access to a bindingcontext without binding?
I have a ObservableCollection of objects (with property body, sender and datetime).
This ObservableCollection is the datasource of a listview. For this listview I've made my own templates (custom viewcell) with different labels, where I bind the propertys of my object to. Till there everything is fine, now the problem:
I want to change the textalignment, depending of the sender (e.g. if the sender is "me", the text should be right aligned, else it should be left aligned) and i want to change the width of the viewcell depending of the length of the body.
So how can I get access to the bindingcontext informations???

the part of the page with the listview:

public static ObservableCollection<Models.Nachricht> ChatListe = new ObservableCollection<Models.Nachricht>();

ChatAnsicht = new ListView()
                ItemsSource = ChatListe,
                ItemTemplate = new DataTemplate(typeof(Cells.ChatItemCell)),
                HasUnevenRows = true,


and thats the custom viewcell:

public ChatItemCell()

            TextAlignment Ausrichtung = TextAlignment.End;

            var lblBody = new Label()
                FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), 
                HorizontalTextAlignment = Ausrichtung,
                TextColor = Color.Black
            lblBody.SetBinding(Label.TextProperty, "Body");

            var lblSender = new Label()
                FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
                HorizontalTextAlignment = Ausrichtung,
                TextColor = Color.Gray,
                FontAttributes = FontAttributes.Italic
            lblSender.SetBinding(Label.TextProperty, "Sender");

            var layout = new StackLayout()
                Opacity = 90,
                Orientation = StackOrientation.Vertical,
                Children = {lblSender, lblBody}

            Frame outerFrame = new Frame()
                Padding = new Thickness(5, 5, 5, 5),
                OutlineColor = Color.Red,
                BackgroundColor = Color.Yellow,
                Content = layout,
                HasShadow = true

            Frame objFrame_Outer = new Frame
                Padding = new Thickness(10, 5, 10, 5),
                Content = outerFrame

            View = objFrame_Outer;


So I want to make it like

if (Bindingcontext.("Sender") == "me")
    Ausrichtung = TextAlignment.End

objFrame.Width = Bindingcontext.("Body").Length + xyz;

Thanks a lot for your help!


  • PaulDistonPaulDiston USUniversity ✭✭✭✭

    Although this doesn't answer your specific question, you should have a look at the DataTemplateSelector as they are probably more appropriate for this kind of thing :-

  • KrizKriz DEMember ✭✭

    Hey Paul, thanks for your help!
    This DataTemplateSelector could help, but i does'nt work... I get the error:

    System.TypeLoadException: Could not load type 'Xamarin.Forms.DataTemplateSelector' from assembly 'Xamarin.Forms.Core, Version=, Culture=neutral, PublicKeyToken=null'.

    And is there any other way to get the binding content?


Sign In or Register to comment.