【问题标题】:Grid Table in WPFWPF 中的网格表
【发布时间】:2011-02-12 14:59:39
【问题描述】:

我需要创建一个网格。它应该是可编辑的
我应该设置行数和列数。
例如

mygrid.RowCount = 3;
mygrid.ColumnCount = 3;

它应该是这样的:

如何将二维数组绑定到 DataGrid?

【问题讨论】:

  • 如何创建这样的网格?
  • Sergey,看看这个帖子:stackoverflow.com/questions/276808/…,尤其是 Meleak 的答案(不是公认的答案)。我认为他提供的东西能满足您的需求。

标签: c# .net wpf wpf-controls


【解决方案1】:

您可以使用 WPF DataGrid 控件。它显示一个单元格网格,这些单元格对应于包含属性(列)的对象(行)集合。您需要提供数据存储 - 对象集合。集合中的对象数量(集合计数)将决定网格中的行数。 DataGrid 支持在 UI 中编辑数据。

此示例定义了三列并将它们绑定到数据对象的 A、B 和 C 属性。

<DataGrid AutoGenerateColumns="False" 
          Height="200" 
          HorizontalAlignment="Left" 
          Name="dataGrid1" 
          VerticalAlignment="Top" 
          Width="200">
    <DataGrid.Columns >
            <DataGridTextColumn Binding="{Binding Path=A}" MinWidth="50" />
            <DataGridTextColumn Binding="{Binding Path=B}" MinWidth="50" />
            <DataGridTextColumn Binding="{Binding Path=C}" MinWidth="50" />
    </DataGrid.Columns>
</DataGrid>

您需要(在代码中或使用数据绑定)将具有这些属性的对象集合分配给 DataGrid 的 ItemsSource 属性,与任何其他 ItemsControl 一样。像这样的:

public partial class MainWindow: Window
{
        public class DataObject
        {
            public int A { get; set; }
            public int B { get; set; }
            public int C { get; set; }
        }

        public MainWindow()
        {
            InitializeComponent();

            var list = new ObservableCollection<DataObject>();
            list.Add(new DataObject() { A = 6, B = 7, C = 5 });
            list.Add(new DataObject() { A = 5, B = 8, C = 4 });
            list.Add(new DataObject() { A = 4, B = 3, C = 0 });
            this.dataGrid1.ItemsSource = list;
}

编辑中心单元格时,结果如下所示:

旁注:WPF Grid 类仅用于布局。它不提供数据编辑支持。

【讨论】:

  • 我需要动态的行数和列数。我不能拥有 100..000 个属性。我必须将二维数组绑定到 DataGrid
  • 所以使用二维集合:List>,并将每一列绑定到数组的一个元素:List> list = new List >(3); list.Add(new List(new int[] { 6, 7, 5 })); (等)this.dataGrid1.ItemsSource = 列表;在 xaml 中,像这样设置列: 然后 Path=[1] 等。您仍然需要编写代码来添加列调整数据大小时的定义,但这并不难。
  • 另一方面,如果您真正想要的是任意 2D 单元矩阵,并且您不想做管理数据存储和 UI 维护的工作,也许您应该改用 Excel ? ;>
【解决方案2】:

以下是创建ItemsControl 的一般技术,该Grid 使用Grid 来布置其项目。在此示例(使用 XML 数据源)中,ItemsSource 是具有 RowColumnData 属性的项的集合。

注意ItemContainerStyle 的使用。这是必要的,因为为了让Grid 控件使用Grid.RowGrid.Column 附加属性,这些属性必须附加到插入到网格中的对象 - 如果您尝试在TextBox 上设置它们ItemsTemplate 正在生成,网格将看不到它们,因为它正在查看生成的 ContentPresenter,而不是其中的 TextBox

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <XmlDataProvider x:Key="Data">
      <x:XData>
        <Data xmlns="">
          <Item Row="0" Column="0" Data="0,0"/>
          <Item Row="1" Column="1" Data="1,1"/>
          <Item Row="2" Column="1" Data="2,1"/>
          <Item Row="3" Column="2" Data="3,2"/>
          <Item Row="4" Column="4" Data="4,4"/>
          <Item Row="4" Column="3" Data="4,3"/>
        </Data>
      </x:XData>
    </XmlDataProvider>
  </Page.Resources>
  <DockPanel>
    <ItemsControl ItemsSource="{Binding Source={StaticResource Data}, XPath=/Data/Item}">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <Grid>  
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="50"/>
              <ColumnDefinition Width="50"/>
              <ColumnDefinition Width="50"/>
              <ColumnDefinition Width="50"/>
              <ColumnDefinition Width="50"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="30"/>    
              <RowDefinition Height="30"/>    
              <RowDefinition Height="30"/>    
              <RowDefinition Height="30"/>    
              <RowDefinition Height="30"/>    
            </Grid.RowDefinitions>
          </Grid>        
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
      <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
          <Setter Property="Grid.Row" Value="{Binding XPath=@Row}"/>
          <Setter Property="Grid.Column" Value="{Binding XPath=@Column}"/>
        </Style>
      </ItemsControl.ItemContainerStyle>
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <TextBox Text="{Binding XPath=@Data, Mode=TwoWay}"/>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </DockPanel>
</Page>

【讨论】:

  • 我需要动态的行数和列数。
  • 内置的Grid 不能做到这一点有点烦人。但不难对其进行子类化以添加此功能 - 请参阅 blog.rag.no/post/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 2021-08-05
相关资源
最近更新 更多