none
在MVVM模式下,listbox采用UniformGrid作为布局方式,listbox如何绑定对象类型的二维数组? RRS feed

  • 问题

  • 二维数组的类型是对象类型,非值类型

    int i=Convert.toInt32(Textbox.Text);

    Thing[,] t=new Thing[i,i],

    Thing实体有一个类型为Color的属性,我想设置指定ListBoxItem的显示颜色(更改二维数组指定下标的Thing对象的

    color属性),首先要知道怎么绑定二维数组,以及绑定的数据更新问题

    ListBox显示效果如扫雷内的方框一样一样,但是我并不是要制作扫雷。



    2017年10月7日 6:06

答案

  • Hi,

    >>首先要知道怎么绑定二维数组,以及绑定的数据更新问题

    据我所知, 你不能直接绑定一维以上的数组到listBox上, 所以你需要一定的转换,比如你可以尝试转换成datatable.

    private DataTable Convert2DArrayToDataTable(double[,] data, string[] columnNames)
    {
        int len1d = data.GetLength(0);
        int len2d = data.GetLength(1);
        Check2DArrayMatchColumnNames(data, columnNames);
    
        DataTable dt = new DataTable();
        for (int i = 0; i < len2d; i++)
        {
            dt.Columns.Add(columnNames[i], typeof(double));
        }
    
        for (int row = 0; row < len1d; row++)
        {
            DataRow dr = dt.NewRow();
            for (int col = 0; col < len2d; col++)
            {
                dr[col] = data[row, col];
            }
            dt.Rows.Add(dr);
        }
    
        return dt;
    }
    
    private void Check2DArrayMatchColumnNames(double[,] data, string[] columnNames)
    {
        int len2d = data.GetLength(1);
    
        if (len2d != columnNames.Length)
        {
            throw new Exception("The second dimensional length must equals column names.");
        }
    }

    你也可以尝试将二维的数组转换成ObservableCollection

    Datas = new ObservableCollection<Thing>( t.Cast<Thing>().ToList());

    以下是测试结果:

    public partial class sample6 : Window
        {
            public ObservableCollection<Thing> Datas { get; set; }
    
            public sample6()
            {
                InitializeComponent();
    
    
                Thing[,] t = new Thing[,] {
                   { new Thing() { data = "[0,0]", bgBrush = null }, new Thing() { data = "[0,1]", bgBrush = null } } ,
                   { new Thing() { data = "[1,0]",  }, new Thing() { data = "[1,1]", bgBrush = null } },
                   { new Thing() { data = "[2,0]", }, new Thing() { data = "[2,1]", bgBrush = null } },
                   { new Thing() { data = "[3,0]", bgBrush = new SolidColorBrush(Colors.Red) }, new Thing() { data = "[3,1]", bgBrush = null } }
                };
    
                Datas = new ObservableCollection<Thing>( t.Cast<Thing>().ToList());
    
                this.DataContext = this;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Datas[1].bgBrush = new SolidColorBrush(Colors.Green);
                Datas.Add(new Thing { data = "[4,0]", bgBrush = new SolidColorBrush(Colors.Gray) });
            }
        }
    
    public class Thing: PropertyNotifyBaseClass
        {
            private string _data;
            public string data
            {
                get { return _data; }
                set
                {
                    _data = value;
                    RaisePropertyChanged("data");
                }
            }
            private SolidColorBrush _bgBrush;
            public SolidColorBrush bgBrush
            {
                get { return _bgBrush; }
                set
                {
                    _bgBrush = value;
                    RaisePropertyChanged("bgBrush");
                }
            }
        }
    }

      <StackPanel>
            <TextBox x:Name="TextBox"></TextBox>
            <Button  Click="Button_Click" Height="20">t</Button>
            <ListBox ItemsSource="{Binding Datas}">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="2"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Button Width="30" Height="30" Background="{Binding bgBrush}" Content="{Binding data}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>


    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年10月9日 4:18
    版主