拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何将Array资料系结到DataGrid,实作资料更新?

如何将Array资料系结到DataGrid,实作资料更新?

白鹭 - 2022-03-11 1956 0 0

我想将 Array 系结到一个 4 列的 DataGrid 并更新资料。这可能吗?

如果不能实作,我需要如何修改它?

对于自动更新,似乎我们可以系结到一些具有可编辑值的物件集合是为该项目定义一个类。但我不知道该怎么做。

 public partial class MainWindow : Window
  {
    int X=18;   
    public MainWindow()
    {
      InitializeComponent();
      int[] arr = new int[] { 61, 61, 61, 61, 61, 20, 30, 40, 50, 100, 200, 300, 400, 500, 0, 0, 0, 0, 0, 18, 23, 65, 41, 22, 91, 64, 33, 18, 44, 63, 91, 26, 32, 61, 83, 91, 26, 32, 91, 91, 91, 91 };
      DataGrid dataGrid = new DataGrid();
      dataGrid.AutoGenerateColumns = false;
      dataGrid.ItemsSource = arr.ToList();
      for (int i = 1; i < 5; i  )
      {
        DataGridTemplateColumn column = new DataGridTemplateColumn();
        column.Header = "Value" i;
        DataTemplate cellTemplate = new DataTemplate();
        FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
        textBlock.SetBinding(TextBlock.TextProperty, new Binding("."));
        cellTemplate.VisualTree = textBlock;
        column.CellTemplate = cellTemplate;

        DataTemplate cellEditingTemplate = new DataTemplate();
        FrameworkElementFactory textBox = new FrameworkElementFactory(typeof(TextBox));
        textBox.SetBinding(TextBox.TextProperty, new Binding("."));
        cellEditingTemplate.VisualTree = textBox;
        column.CellEditingTemplate = cellEditingTemplate;

        dataGrid.Columns.Add(column);
      }
      canvas.Children.Add(dataGrid);
      Canvas.SetLeft(dataGrid, X);
    }
  }

结果:

如何将Array资料系结到DataGrid,实作资料更新?

我想要这样的结果:

61, 61, 61, 61, 61,

20, 30, 40, 50, 100,

200, 300, 400, 500, 0,

0, 0, 0, 0, 18,

uj5u.com热心网友回复:

您只能使用阵列来完成。请注意,这ItemsControl.ItemsSource是型别IEnumerable:您可以将阵列直接分配给此属性。
这里的ToList()呼叫是多余的。
也用aDataGridTextColumn代替了DataGridTemplateColumn取消对单元格模板的修改,只添加了一个TextBlock.

DataGrid将其IEnumerable源集合的每个项目视为一行。这就是为什么您的表格看起来如此:每个数字都写入一个新行。对每一列重复此操作。

这意味着,您必须更改资料结构以适应每项到一行规则。因为您有五列,所以您必须构造阵列以提供 5 元组(五元组)元素 - 每个五元组元素为一行。
您可以通过使用锯齿状阵列(即阵列阵列)来实作这一点。

然后在配置 时DataGrid.Columns,您必须使用系结索引器才能从阵列(行资料)中提取列的值。

您必须知道使用阵列时没有“自动更新”。如果要在应用程序运行时修改阵列(动态资料),则必须使用ObservableCollection<int[]>代替int[][]锯齿状阵列。

主视窗.xaml.cs

public partial class MainWindow : Window
{
  public MainWindow()
  {
    InitializeComponent();

    this.Loaded  = onl oaded;
  }

  private void OnLoaded(object sender, RoutedEventArgs e)
  {
    int[][] numbers = 
    { 
      new[] { 61, 61, 61, 61, 61 }, 
      new[] { 20, 30, 40, 50, 100 },
      new[] { 200, 300, 400, 500, 0 } 
    };
        
    this.NumbersDataGrid.ItemsSource = numbers;
 }
}

主视窗.xaml

<Window>
    <DataGrid x:Name="NumbersDataGrid"
              AutoGenerateColumns="False">
    <DataGrid.Columns>
      <DataGridTextColumn Header="Value1"
                          Binding="{Binding [0]}" />
      <DataGridTextColumn Header="Value2"
                          Binding="{Binding [1]}" />
      <DataGridTextColumn Header="Value3"
                          Binding="{Binding [2]}" />
      <DataGridTextColumn Header="Value4"
                          Binding="{Binding [3]}" />
      <DataGridTextColumn Header="Value4"
                          Binding="{Binding [4]}" />
    </DataGrid.Columns>
  </DataGrid>
</Window>
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *