2017-06-27 9 views
0

UWPアプリケーションでItemControlというカスタムコントロールを作成しました。UWPがカスタムコントロールをプログラムで追加しています:データが表示されない

コントロールの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 ItemControl(Item item) 
{ 
    this.InitializeComponent(); 

    textBox = new TextBox() { Text = item.itemName }; 
    textBox_status = new TextBox() { Text = item.itemStatus }; 
    textBox_type = new TextBox() { Text = item.itemType }; 
    textBox_name = new TextBox() { Text = item.itemName }; 
    textBox1 = new TextBox() { Text = "Item Details" }; 
    textBox2 = new TextBox() { Text = "Inventory number: " + item.inventoryNumber }; 
    textBox3 = new TextBox() { Text = "Purchase date: " + item.purchaseDate }; 
    textBox4 = new TextBox() { Text = "Purchase value: " + item.purchaseValue }; 
    textBox5 = new TextBox() { Text = "Location: " + item.locationId }; 
    textBox6 = new TextBox() { Text = "Person in charge: " + item.personId }; 
} 

私はプログラム的にこのように、アイテムページのスタックパネルにこれらのコントロールを追加してい:

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(); 
} 

しかし、アプリケーションを実行すると、th項目コントロールは空白のテキストボックスで表示されます。

+0

があなたの '' CustomControl' UserControl'ですか?または 'TemplatedControl'? – AVK

+0

@AVK A 'UserControl'。 – Eutherpy

+0

あなたのUserコントロールで、XAMLにこれらのテキストボックスがありますか? – AVK

答えて

2

あなたのコードでは、多数のコントロールが作成され、メンバー変数に割り当てられますが、それらは親コントロールに追加されません。親には既にコントロールが存在するため、新しいコントロールを作成する必要はありません。

あなたが最も可能性が高いので、のような既存のもの、に値を割り当てる:

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

    textBox.Text = item.itemName; 
    ... 
} 
関連する問題