首页 文章

在datagridView Rows Wpf中添加动态按钮

提问于
浏览
0

我正在使用WPF应用程序 . 我需要使用一些属性动态创建按钮,然后在dataGridView Rows中添加按钮 . 但是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();

2 回答

  • 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>
    
  • 3

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

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

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

    请检查它是否有效 .

    由于我没有做任何实施,你必须处理 property 变化

相关问题