【问题标题】:Add Dynamic Button in datagridView Rows Wpf在datagridView行Wpf中添加动态按钮
【发布时间】:2014-10-25 12:12:27
【问题描述】:

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

这是我的代码:

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();

【问题讨论】:

  • 好吧,如果“数据表不包含”某些内容,则创建一个包含所需数据的自定义对象集合并将其分配给ItemsSource。无论如何,在 WPF 应用程序中很少有 DataTable 的位置。如需更多信息和示例,请查看MSDN-DataGrid

标签: c# wpf c#-4.0 wpf-controls wpfdatagrid


【解决方案1】:

听起来您还没有遇到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>

【讨论】:

    【解决方案2】:

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

    <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; }
    }
    

    请检查它是否有效。

    您必须注意属性更改,因为我没有进行任何实现

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2012-08-09
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      相关资源
      最近更新 更多