Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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# 如何显示ItemsControl中每个项目的编号?_C#_.net_Wpf_Xaml - Fatal编程技术网

C# 如何显示ItemsControl中每个项目的编号?

C# 如何显示ItemsControl中每个项目的编号?,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我在xaml中将此列表分配给itemscontrol,我想显示列表中每个项目的编号: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); string[] assignments = new string[] { "https://cdn2.iconfinder.com/data/icons/animals/48/Tu

我在xaml中将此列表分配给itemscontrol,我想显示列表中每个项目的编号:

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        string[] assignments = new string[] { "https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Butterfly.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Dolphin.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Panda.png" };
        Random rnd = new Random();

        string[] randomingArray = assignments.OrderBy(x => rnd.Next()).ToArray();

        string repeatNumber = "";

        List<string> animals = new List<string>(); 

        for (int i = 1; i < 100; i++)
        {
            if (i == 9)
            {
                repeatNumber = randomingArray[i % randomingArray.Length];
                animals.Add(repeatNumber);
            }
            else if ((i % 9) == 0)
            {
                animals.Add(repeatNumber);
            }
            else
            {
                animals.Add(randomingArray[i % randomingArray.Length]);
            }
            ItemsControl1.ItemsSource = animals;
        }
    }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
字符串[]分配=新字符串[]{”https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Butterfly.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Dolphin.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Panda.png" };
随机rnd=新随机();
string[]randomingArray=assignments.OrderBy(x=>rnd.Next()).ToArray();
字符串repeatNumber=“”;
列出动物=新列表();
对于(int i=1;i<100;i++)
{
如果(i==9)
{
repeatNumber=randomingArray[i%randomingArray.Length];
动物。添加(重复编号);
}
如果((i%9)==0),则为else
{
动物。添加(重复编号);
}
其他的
{
添加(随机化阵列[i%随机化阵列.长度]);
}
ItemsControl 1.ItemsSource=动物;
}
}
}
xaml的代码:

<ListBox x:Name="ItemsControl1" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="2" Width="Auto" Height="Auto">
                    <Image Source="{Binding}" Margin="0,0,5,0"/>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

基本上,应该有带图片的盒子,编号从1到99


我已经尝试了List.count方法和其他涉及xaml的方法,但是我无法让它工作。

创建一个以数字和图像源为属性的类,并更新ItemTemplate。请参阅下面的代码

<ListBox x:Name="ItemsControl1" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="2" Width="Auto" Height="Auto">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Number}"/>
                        <Image Source="{Binding Source}" Margin="0,0,5,0"/>
                    </StackPanel>

                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        string[] assignments = new string[] { "https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Butterfly.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Dolphin.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Panda.png" };
        Random rnd = new Random();

        string[] randomingArray = assignments.OrderBy(x => rnd.Next()).ToArray();

        string repeatNumber = "";

        List<ImageSource> animals = new List<ImageSource>();

        for (int i = 1; i < 100; i++)
        {
            if (i == 9)
            {
                repeatNumber = randomingArray[i % randomingArray.Length];
                animals.Add(new ImageSource(){ Source = repeatNumber, Number=i });
            }
            else if ((i % 9) == 0)
            {
                animals.Add(new ImageSource() { Source = repeatNumber, Number = i });
            }
            else
            {
                animals.Add(new ImageSource() { Source = randomingArray[i % randomingArray.Length], Number = i });
            }
            ItemsControl1.ItemsSource = animals;
        }
    }
}

class ImageSource
{
    public int Number { get; set; }
    public string Source { get; set; }
}

公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
字符串[]分配=新字符串[]{”https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Butterfly.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Dolphin.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Panda.png" };
随机rnd=新随机();
string[]randomingArray=assignments.OrderBy(x=>rnd.Next()).ToArray();
字符串repeatNumber=“”;
列出动物=新列表();
对于(int i=1;i<100;i++)
{
如果(i==9)
{
repeatNumber=randomingArray[i%randomingArray.Length];
Add(newImageSource(){Source=repeatNumber,Number=i});
}
如果((i%9)==0),则为else
{
Add(newImageSource(){Source=repeatNumber,Number=i});
}
其他的
{
Add(newImageSource(){Source=randomingArray[i%randomingArray.Length],Number=i});
}
ItemsControl 1.ItemsSource=动物;
}
}
}
类图像源
{
公共整数{get;set;}
公共字符串源{get;set;}
}

创建一个ValueConverter,它可以计数并显示该值,而不管绑定到什么值都可以工作。或者将其存储为带字符串的结构。