C# UWP以编程方式添加自定义控件:数据不显示

C# UWP以编程方式添加自定义控件:数据不显示,c#,.net,uwp,custom-controls,C#,.net,Uwp,Custom Controls,我在UWP应用程序中创建了一个名为ItemControl的自定义控件 控件的XAML: <UserControl x:Class="UniBL.ItemControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UniBL"

我在UWP应用程序中创建了一个名为
ItemControl
的自定义控件

控件的XAML:

<UserControl
    x:Class="UniBL.ItemControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UniBL"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="283.385" Width="430.462">

    <Grid Margin="0,0,-34,0.333">
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="0,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="235" Foreground="#FF3868E6" BorderThickness="0" IsTabStop="False" Background="{x:Null}"/>
        <TextBox x:Name="textBox_status" HorizontalAlignment="Left" Margin="0,39,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="109" Foreground="#FF3868E6" BorderThickness="1,0" IsTabStop="False" Background="{x:Null}" BorderBrush="#FFDAD9D9" Height="32"/>
        <TextBox x:Name="textBox_type" HorizontalAlignment="Left" Margin="126,39,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="109" Foreground="#FF3868E6" BorderThickness="1,0" IsTabStop="False" Background="{x:Null}" BorderBrush="#FFDAD9D9"/>
        <TextBox x:Name="textBox_name" HorizontalAlignment="Left" Margin="250,39,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="109" Foreground="#FF3868E6" BorderThickness="1,0" IsTabStop="False" Background="{x:Null}" BorderBrush="#FFDAD9D9"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Margin="9,76,0,0" TextWrapping="Wrap" Text="Item Details" VerticalAlignment="Top" Background="#66C9D6CA" IsTabStop="False"/>
        <StackPanel HorizontalAlignment="Left" Height="160" Margin="10,113,0,0" VerticalAlignment="Top" Width="414" BorderThickness="1" BorderBrush="#FFB2AAAA">
            <TextBox x:Name="textBox2" TextWrapping="Wrap"/>
            <TextBox x:Name="textBox3" TextWrapping="Wrap"/>
            <TextBox x:Name="textBox4" TextWrapping="Wrap"/>
            <TextBox x:Name="textBox5" TextWrapping="Wrap"/>
            <TextBox x:Name="textBox6" TextWrapping="Wrap"/>
        </StackPanel>
    </Grid>
</UserControl>
我以编程方式将这些控件添加到“项目”页面上的堆栈面板中,如下所示:

public Items()
{
     ...
     List<Item> items = Item.ReadItems(); //the data is loaded correctly
     foreach (Item item in items)
            isp.Children.Add(new ItemControl(item));
     isp.UpdateLayout();
}
公共项目()
{
...
List items=Item.ReadItems();//数据加载正确
foreach(项目中的项目)
isp.Children.Add(新项控件(项));
isp.UpdateLayout();
}

但是,当我运行应用程序时,项目控件显示为空白文本框

您的代码创建一组控件,将它们分配到成员变量中,但不将它们添加到父控件中。父控件上也已存在控件,因此无需创建新控件

您很可能希望将值指定给现有值,如下所示:

public ItemControl(Item item)
{
  this.InitializeComponent();

  textBox.Text = item.itemName;
  ...
}

您的代码创建一组控件,将它们分配到成员变量中,但不将它们添加到父控件中。父控件上也已存在控件,因此无需创建新控件

您很可能希望将值指定给现有值,如下所示:

public ItemControl(Item item)
{
  this.InitializeComponent();

  textBox.Text = item.itemName;
  ...
}

您的
CustomControl
UserControl
吗?或者是
模板控件
?@AVK a
用户控件
。那么在您的用户控件中,这些文本框是否在XAML中?@AVK是的,我将编辑我的问题。另外,您不需要在用户控件上手动分配它们。只需在XAML上使用
x:Bind
。您的
CustomControl
a
UserControl
?或者是
模板控件
?@AVK a
用户控件
。那么在您的用户控件中,这些文本框是否在XAML中?@AVK是的,我将编辑我的问题。另外,您不需要在用户控件上手动分配它们。只需在XAML上使用
x:Bind