0

I got a List and I want "join" two binding properties to display in a listview. Something like:

public class T
{
    public string nit { get; set; }
    public string nombrecliente { get; set; }
    public string nombresitio { get; set; }
    public string direccion { get; set; }
}

and the listview class is something like:

    Label lblTitulo, lblSubTitulo;
    listview.ItemsSource = List<T>;
    T objT = new T();

    string m_titulo = "";
    string m_subtitulo = "";

    public lvClientes ()
    {
        m_titulo = agregarTexto (m_titulo, "NIT: " + objT.nit);
        m_titulo = agregarTexto (m_titulo, "Cliente: " + objT.nombrecliente);


        m_subtitulo = agregarTexto (m_titulo, "Sitio: " + objT.nombresitio);
        m_subtitulo = agregarTexto (m_titulo, "Dirección: " + objT.direccion);

        lblSubTitulo.SetBinding (Label.TextProperty, m_subtitulo);
        lblTitulo.SetBinding(Label.TextProperty, m_titulo;
    }

    private string agregarTexto (string textoOriginal, string textoAgregar)
    {
        if (!textoAgregar.Equals (""))
        {
            if (textoOriginal.Equals (""))
            {
                textoOriginal = textoAgregar.Replace ("\n", ", ");
            }
            else
                textoOriginal += "\n" + textoAgregar.Replace ("\n", ", ");
        }
        return textoOriginal;
    }

But, that way I get a NullArgumentException adding m_titulo & m_subtitulo to a labels. Can help me?

Samir Ríos
  • 157
  • 1
  • 2
  • 9
  • I disagree that this is an exact duplicate. The referenced question lists many root causes of null reference exceptions, but does not deal with this one -- which is what happens if you use Xamarin data binding to bind to a name that is not a member of the binding context type. As a result, none of the remedies described in that post are applicable for the OP's situation. – DavidS Jun 15 '16 at 15:41

2 Answers2

0

You can only bind to properties of the class T. So in your case, you could modify T to be something like:

public class T
{
    public string nit { get; set; }
    public string nombrecliente { get; set; }
    public string nombresitio { get; set; }
    public string direccion { get; set; }
    public string titulo {
        get {
            var temp = agregarTexto ("", "NIT: " + nit);
            return agregarTexto (temp, "Cliente: " + nombrecliente);
        }
    }
}

And then in lvClientes, the binding for lblTitulo would be:

   lblTitulo.SetBinding(Label.TextProperty, "titulo");

You would have to define a similar property for subtitulo. You may also want to cache the computed strings to avoid recomputing as the user scrolls through the list. But this mechanism of adding properties will do what you want.

DavidS
  • 2,904
  • 1
  • 15
  • 21
0
price.SetBinding (Label.TextProperty,new Binding ("Totalprice", stringFormat: "{0} €"));

This can be interesting for you..

Mr.Koçak
  • 313
  • 4
  • 9