2014-09-01 69 views
0

我正在使用WPF应用程序。我需要用一些属性动态创建按钮,并在dataGridView行中添加按钮。但Datatable不包含按钮DataType那么如何做到这一点请有人帮助我提前感谢。在datagridView行中添加动态按钮Wpf

这是我的代码:

DataTable NewDataTable = new DataTable(); 
for (int i = 0; i < Row * level; i++) 
{ 
    Button[] buttonArray = new Button[position]; 
    string Col1 = "Rows"; 
    string Col2 = "Levels"; 
    for (int j = 0; j < position; j++) 
    {      
     if (j == 1) 
     { 
      Col1 = "Rows"; 
     } 
     else if (j == 2) 
     { 
      Col2 = "Levels"; 
     } 
     else 
     { 
      buttonArray[j] = new Button(); 
      buttonArray[j].Content = "Postion " + j; 
      buttonArray[j].ToolTip = "Postion " + j; 

     }      
    } 
    NewDataTable.Rows.Add(Col1, Col2, buttonArray[j]); 
} 

代码

Dgrid.ItemsSource = NewDataGrid.DefaultView(); 
+0

那么,如果“数据表不包含”的东西,然后创造出有所需的数据,并将其分配给'ItemsSource'自定义对象的集合。无论如何,在WPF应用中很少有DataTable的地方。有关更多信息和示例,请查看[MSDN-DataGrid](http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid%28v=vs.110%29.aspx)。 – icebat 2014-09-01 10:19:08

回答

3

听起来好像你还没有遇见DataGridTemplateColumn Class照顾性质的变化。让我从链接页面介绍给你,可以包含任何控制列...:

表示一个DataGrid列承载在其细胞模板指定的内容。

一个例子,再次从链接页面:

<Grid> 
    <Grid.Resources> 
     <!--DataTemplate for Published Date column defined in Grid.Resources. PublishDate is a property on the ItemsSource of type DateTime --> 
     <DataTemplate x:Key="DateTemplate" > 
      <StackPanel Width="20" Height="30"> 
       <Border Background="LightBlue" BorderBrush="Black" BorderThickness="1"> 
        <TextBlock Text="{Binding PublishDate, StringFormat={}{0:MMM}}" FontSize="8" HorizontalAlignment="Center" /> 
       </Border> 
       <Border Background="White" BorderBrush="Black" BorderThickness="1"> 
        <TextBlock Text="{Binding PublishDate, StringFormat={}{0:yyyy}}" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" /> 
       </Border> 
      </StackPanel> 
     </DataTemplate> 
     <!--DataTemplate for the Published Date column when in edit mode. --> 
     <DataTemplate x:Key="EditingDateTemplate"> 
      <DatePicker SelectedDate="{Binding PublishDate}" /> 
     </DataTemplate> 
    </Grid.Resources> 
    <DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" > 
     <DataGrid.Columns> 
      <!--Custom column that shows the published date--> 
      <DataGridTemplateColumn Header="Publish Date" CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}" /> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 
1

我创建了一个工作示例后面。以下是代码

<DataGrid x:Name="dtGrid" AutoGenerateColumns="False" IsReadOnly="True"> 
    <DataGrid.Columns> 
    <DataGridTextColumn Header="Col1" Binding="{Binding Col1}" /> 
    <DataGridTextColumn Header="Col2" Binding="{Binding Col2}" /> 
     <DataGridTemplateColumn Header="ButtonsList"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <Button Content="{Binding ButtonsList.Content}" ToolTip="{Binding ButtonsList.ToolTip}"/> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

这是数据网格的Xaml部分。

后面的代码文件

public MainWindow() 
    { 
     InitializeComponent(); 

     DataTable dt = new DataTable(); 
     dt.Columns.Add(new DataColumn("Col1", Type.GetType("System.String"))); 
     dt.Columns.Add(new DataColumn("Col2", Type.GetType("System.String"))); 
     dt.Columns.Add(new DataColumn("ButtonsList", Type.GetType("WpfApplication.ButtonsList"))); 
     dt.Rows.Add("Test1", "Test1", new ButtonsList { Content = "Test1", ToolTip = "Test1" }); 
     dt.Rows.Add("Test2", "Test2", new ButtonsList { Content = "Test2", ToolTip = "Test2" }); 
     dtGrid.ItemsSource = dt.DefaultView; 

    } 

正如你可以看到,我创建了一个新的类为您的按钮。如果您直接将按钮添加到DataGrid,则会遇到问题。额外的类是非常简单的

public class ButtonsList 
{ 
    public String Content { get; set; } 
    public string ToolTip { get; set; } 
} 

请检查它是否工作。

你,因为我没有做任何实现