Binding a List in Code-Behind? Silverlight

1

Would anyone know how I can bind a generic list in the Behind code?

I had to create my Expanderview by code-behind and I have to do the binding of a property from my list for a Grid to update according to the color I set in ViewModel.

Follow the code below:

    private List<InventarioDto> _lstInventario;
    public List<InventarioDto> LstInventario
    {
        get { return _lstInventario; }
        set
        {
            _lstInventario = value;
            Deployment.Current.Dispatcher.BeginInvoke(() => RaisePropertyChanged("LstInventario"));
        }

My Class:

public class InventarioDto : ObservableObject
{

    private string CorField;

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Cor
    {
        get
        {
            return this.CorField;
        }
        set
        {
            if ((object.ReferenceEquals(this.CorField, value) != true))
            {
                this.CorField = value;
                this.RaisePropertyChanged("Cor");
            }
        }
    }

}

}

Load event that loads the stackePanel:

    public async void LstTreeView_Loaded(object sender, RoutedEventArgs e)
    {


        try
        {

            int id = int.Parse(ViewModel.IdLocal);

            await ViewModel.GetItensPorLocal(id);


            for (int i = 0; i < ViewModel.LstTreeView.Count(); i++)
            {
                ExpanderView expanderView = new ExpanderView();

                expanderView.Header = ViewModel.LstTreeView[i].Itens;
                expanderView.FontSize = 26;

                long[] result = ViewModel.LstInventario.OrderBy(x => x.NumeroPatrimonio).Where(x => x.Itens == expanderView.Header.ToString()).Select(x => x.NumeroPatrimonio).ToArray();

                for (int a = 0; a < result.Length; a++)
                {
                    Grid grid = new Grid();
                    grid.Name = $"txtTreeView{i}{a}";

                    InventarioDto model = ViewModel.LstInventario[a];

                    grid.SetBinding(Grid.BackgroundProperty, new Binding("Cor")
                    {
                        Source = model,
                        UpdateSourceTrigger = UpdateSourceTrigger.Default,

                    });

                    TextBlock BlockTreeView = new TextBlock();

                    BlockTreeView.FontSize = 26;
                    BlockTreeView.Text = "Nº Patrimônio: " + result[a].ToString();

                    grid.Children.Add(BlockTreeView);
                    expanderView.Items.Add(grid);

                }

                stckTreeView.Children.Add(expanderView);

            }

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }


    }

My sham:

                    <StackPanel Name="stckTreeView" Loaded="LstTreeView_Loaded">

                    </StackPanel>
    
asked by anonymous 03.11.2016 / 18:43

0 answers