在datagridView行Wpf中添加动态按钮

suzh9iv8  于 2023-05-13  发布在  其他
关注(0)|答案(2)|浏览(268)

我正在使用WPF应用程序。我需要创建按钮动态与一些属性,我添加在dataGridView行按钮。但是数据表不包含按钮数据类型,那么如何做到这一点,请有人帮助我,提前感谢。
这是我的代码:

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

sulc1iza1#

听起来你好像还没见过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>
pexxcrt2

pexxcrt22#

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

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

请检查它是否工作。
你必须照顾的财产变化,因为我还没有做任何实现

相关问题