Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在标签代码后面绑定列表元素_C#_Wpf_Data Binding - Fatal编程技术网

C# 在标签代码后面绑定列表元素

C# 在标签代码后面绑定列表元素,c#,wpf,data-binding,C#,Wpf,Data Binding,我正在尝试用动态创建的标签绑定结构列表 结构代号: public struct PrinterToGridBinds { public string extPrinterName { get; set; } public string extIcecreamType { get; set; } public string extBatchNumber { get; set; } pub

我正在尝试用动态创建的标签绑定结构列表

结构代号:

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>();
并将此属性添加到绑定中,如下所示:

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

一切都很有魅力!我的错误在哪里?如何用标签动态绑定结构数据?

对于您的工作示例(使用
PropName
),您在属性路径中定义了属性名称,但对于打印机,您定义了属性值
lst\u PrinterToGridBindings[i]。extPrinterName
。看一看房子。您还应该为绑定指定
为了显示元素列表,请使用ItemsControl。将其ItemsSource属性分配或绑定到项对象集合。将其ItemTemplate设置为具有绑定到item类属性的UI元素的DataTemplate

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

有关详细信息,请参阅。

谢谢您的回答。我试着用引号像name那样做,但这不起作用,因为变量“I”。@Alex您还应该为绑定指定
源代码,哦,是的!我完全忘了这件事。将我的代码更改为
Binding lbl\u Binding=new Binding{Source=lst\u PrinterToGridBindings[i],Path=new PropertyPath(“extPrinterName”),Mode=BindingMode.OneWay},这是正常工作的。非常感谢!
public string PropName1 { get; set; } = "PropTest";
Binding lbl_Binding = new Binding {Path = new PropertyPath("PropName1"), Mode = BindingMode.OneWay };
<ItemsControl x:Name="printerList">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding extPrinterName}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
printerList.ItemsSource = lst_PrinterToGridBindings;