Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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/9/ssl/3.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
WPF-将二维整数数组显示为表格_Wpf_Multidimensional Array - Fatal编程技术网

WPF-将二维整数数组显示为表格

WPF-将二维整数数组显示为表格,wpf,multidimensional-array,Wpf,Multidimensional Array,我有int[40,40],包含1和0 我想创建一个二维表格40x40,这样一个绿色的正方形代表1,红色的正方形代表0 我还想为每个单元格添加工具提示,并显示索引 大概是这样的: 怎么做?我会先创建一个锯齿状数组(我认为绑定多维数组不是一个好方法,因为它不是IEnumerable)。然后可以执行简单的两级嵌套项控件,例如: <ItemsControl ItemsSource="{Binding Items}"> <ItemsControl.ItemTemplate>

我有
int[40,40]
,包含1和0

我想创建一个二维表格40x40,这样一个绿色的正方形代表1,红色的正方形代表0

我还想为每个单元格添加工具提示,并显示索引

大概是这样的:


怎么做?

我会先创建一个锯齿状数组(我认为绑定多维数组不是一个好方法,因为它不是IEnumerable)。然后可以执行简单的两级嵌套项控件,例如:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Bits}">
                <ItemsControl.Resources>
                    <converters:BoolToBrushConverter x:Key="BoolToBrush" TrueBrush="Green" FalseBrush="Red" />
                </ItemsControl.Resources>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Rectangle Width="10" Height="10" 
                            Fill="{Binding Converter={StaticResource BoolToBrush}}"
                            Stroke="White" StrokeThickness="1"
                        />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
编辑

我使用锯齿状数组
bool[][]
对此进行了测试,并遇到了一些奇怪的行为——似乎运行时正在将每个嵌套的
bool[]
转换为字符串。因此,我创建了一个包装类“BitArray”,并将“Items”设置为一种类型
BitArray[]
,它按预期工作

public class BitArray
{
    public bool[] Bits { get; set; }

    public BitArray(params bool[] args)
    {
        Bits = args;
    }
}
测试数据:

Items = new BitArray[]
{
    new BitArray(true, false, true, true),
    new BitArray(false, false, true, false),
    new BitArray(false, true, true, false),
    new BitArray(true, false, true, false),
};


编辑#2注意,由于您希望将索引显示为工具提示,我建议创建另一个包装类来保存内部bool,以及当前的(I,j)索引。

我将首先创建一个锯齿数组(我认为绑定多维数组不是一个好方法,因为它不是IEnumerable)。然后可以执行简单的两级嵌套项控件,例如:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Bits}">
                <ItemsControl.Resources>
                    <converters:BoolToBrushConverter x:Key="BoolToBrush" TrueBrush="Green" FalseBrush="Red" />
                </ItemsControl.Resources>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Rectangle Width="10" Height="10" 
                            Fill="{Binding Converter={StaticResource BoolToBrush}}"
                            Stroke="White" StrokeThickness="1"
                        />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
编辑

我使用锯齿状数组
bool[][]
对此进行了测试,并遇到了一些奇怪的行为——似乎运行时正在将每个嵌套的
bool[]
转换为字符串。因此,我创建了一个包装类“BitArray”,并将“Items”设置为一种类型
BitArray[]
,它按预期工作

public class BitArray
{
    public bool[] Bits { get; set; }

    public BitArray(params bool[] args)
    {
        Bits = args;
    }
}
测试数据:

Items = new BitArray[]
{
    new BitArray(true, false, true, true),
    new BitArray(false, false, true, false),
    new BitArray(false, true, true, false),
    new BitArray(true, false, true, false),
};

编辑#2注意,由于您希望将索引显示为工具提示,我建议创建另一个包装器类来保存内部bool,以及当前的(I,j)索引。

这项工作很棒。 为了工具提示,我像你说的那样做了一点修改

这是最后的代码

1位类

public class Bit
{
    public string Index { get; set; }
    public bool Value { get; set; }
}
一行的类(40位)

公共类比特数组
{
公共位[]位{get;set;}
公共比特数()
{
位=新位[40];
对于(int i=0;i<40;i++)
位[i]=新位();
}
}
最后是40x40阵列

BitsArray[] MyBits= new BitsArray[40];
for (int i = 0; i < 40; i++)
    MyBits[i] = new BitsArray();
BitsArray[]MyBits=新的BitsArray[40];
对于(int i=0;i<40;i++)
MyBits[i]=新比特数组();
xaml

<DataTemplate>
    <Rectangle Width="10" Height="10"
    Fill="{Binding Path=Value, Converter={StaticResource BoolToBrush}}"
    ToolTip="{Binding Path=Index}" Stroke="White" StrokeThickness="1"/>
</DataTemplate>

工作很好。 为了工具提示,我像你说的那样做了一点修改

这是最后的代码

1位类

public class Bit
{
    public string Index { get; set; }
    public bool Value { get; set; }
}
一行的类(40位)

公共类比特数组
{
公共位[]位{get;set;}
公共比特数()
{
位=新位[40];
对于(int i=0;i<40;i++)
位[i]=新位();
}
}
最后是40x40阵列

BitsArray[] MyBits= new BitsArray[40];
for (int i = 0; i < 40; i++)
    MyBits[i] = new BitsArray();
BitsArray[]MyBits=新的BitsArray[40];
对于(int i=0;i<40;i++)
MyBits[i]=新比特数组();
xaml

<DataTemplate>
    <Rectangle Width="10" Height="10"
    Fill="{Binding Path=Value, Converter={StaticResource BoolToBrush}}"
    ToolTip="{Binding Path=Index}" Stroke="White" StrokeThickness="1"/>
</DataTemplate>