簡體   English   中英

如何在xaml中自動為數組的每個元素創建框?

[英]How do you create box for each element of array automatically in xaml?

我一直在想-如何在xaml中為數組的每個元素自動創建框? 可以說我的代碼的數組總共包含99個元素,我希望每個元素都包含這個50px * 50px的小框。 當然,我不認為正確的方法是另外創建99個小盒子並將它們分配給數組。

到目前為止,我已經研究了數據綁定和ItemsControl,但是找不到足夠好的示例供我遵循。

 string[] assignments = new string[] { "A", "B", "C", "D", "E", "F" };
 Random rnd = new Random();
 string[] randomingArray = assignments.OrderBy(x => rnd.Next()).ToArray();

 string repeatNumber = "";

 for (int i = 1; i < 100; i++)
 {
      if (i == 9)
      {
         repeatNumber = randomingArray[i % randomingArray.Length];
         Console.WriteLine(repeatNumber);

      }
      else if ((i % 9) == 0)
      {
         Console.WriteLine(repeatNumber);
      }
      else
      {
         Console.WriteLine(randomingArray[i % randomingArray.Length]);
      }

 }

我該怎么做呢?

我會說嘗試更基本的WPF教程。 我建議也看一些MVVM教程。 現在請看下面的代碼。

<Grid>       
    <ListBox x:Name="ItemsControl1" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Aqua" BorderThickness="2" Width="Auto" Height="Auto" >
                    <TextBlock Text="{Binding}" Margin="10"/>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ItemsControl1.ItemsSource = new string[] { "A", "B", "C", "D" };
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM