0

I'm try to bind struct list with labels that created dynamically.

code of structure:

public struct PrinterToGridBinds
        {
            public string extPrinterName { get; set; }
            public string extIcecreamType { get; set; }
            public string extBatchNumber { get; set; }
            public string extBeginingDate { get; set; }
            public string extPrinterState { get; set; }
            public string extBatchCounter { get; set; }
            public string extDIOCounter { get; set; }

            public PrinterToGridBinds(string extPrinterName, string extIcecreamType, string extBatchNumber, string extBeginingDate,
                string extPrinterState, string extBatchCounter, string extDIOCounter)
            {
                this.extPrinterName =  extPrinterName;
                this.extIcecreamType = extIcecreamType;
                this.extBatchNumber = extBatchNumber;
                this.extBeginingDate = extBeginingDate;
                this.extPrinterState = extPrinterState;
                this.extBatchCounter = extBatchCounter;
                this.extDIOCounter = extDIOCounter;
            }
        }

        public List<PrinterToGridBinds> lst_PrinterToGridBindings = new List<PrinterToGridBinds>();

Initialize list of structures

private void PrinterToGridBindingInit()
        {
            for (int i = 0; i < PrinterNumber; i++)
            {
                lst_PrinterToGridBindings.Add(new PrinterToGridBinds("Num " + (i + 1).ToString(), "-", "-", "-", "-", "-", "-"));
            }
        }

and try to bind this with labels that creating dynamically. Code below:

for (int i = 0; i < 8; i++)
            {
                Label lbl_PrinterName = new Label();
                lbl_PrinterName.Name = "Name_" + i.ToString();
                Binding lbl_Binding = new Binding {Path = new PropertyPath(lst_PrinterToGridBindings[i].extPrinterName), Mode = BindingMode.OneWay };               
                lbl_PrinterName.SetBinding(Label.ContentProperty, lbl_Binding);

                Grid.SetRow(lbl_PrinterName, i + 2);
                Grid.SetColumn(lbl_PrinterName, 3);
                grd_WorkArea.Children.Add(lbl_PrinterName);
            }

And I don't see any result after program runs. From other side, if I define property

public string PropName1 { get; set; } = "PropTest";

and add this property to binding like this:

Binding lbl_Binding = new Binding {Path = new PropertyPath("PropName1"), Mode = BindingMode.OneWay };

All works like a charm! Where is my mistake?How to bind structure data with labels dynamically?

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Alex
  • 685
  • 3
  • 9
  • 20

2 Answers2

1

Seems like, for your working sample (with PropName) you define the property name in property path, but for printers you define a property value lst_PrinterToGridBindings[i].extPrinterName. Have a look at Binding.Path property. You also should specify the Source for binding

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
  • thank you for the answer. I tried do it like name with quotes, but this is doesn't work because of variable "i". – Alex Mar 27 '19 at 11:05
  • @Alex You also should specify the `Source` for binding, according [this](https://stackoverflow.com/questions/7525185/how-to-set-a-binding-in-code) – Pavel Anikhouski Mar 27 '19 at 11:11
  • 1
    Oh, yes! I'm forget about this absolutely. Changed my code to `Binding lbl_Binding = new Binding {Source = lst_PrinterToGridBindings[i], Path = new PropertyPath("extPrinterName"), Mode = BindingMode.OneWay };` And this is works properly. Many thanks! – Alex Mar 27 '19 at 11:43
0

In order to show a list of elements, use an ItemsControl. Assign or bind its ItemsSource property to a collection of item objects. Set its ItemTemplate to a DataTemplate with UI elements that bind to properties of the item class.

<ItemsControl x:Name="printerList">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding extPrinterName}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Code behind:

printerList.ItemsSource = lst_PrinterToGridBindings;

For details, see Data Templating Overview.

Clemens
  • 123,504
  • 12
  • 155
  • 268