在datagrid wpf中的每一行上添加动态按钮

时间:2016-07-25 09:33:48

标签: c# wpf xaml datagrid

我的DataGrid页面中xaml下面有<{1}}:

<DataGrid x:Name="DataCDetails" AlternationCount="2" HorizontalAlignment="Center" Margin="10,233,10,0" AutoGenerateColumns="False" VerticalAlignment="Top" Height="482" ItemsSource="{Binding Results}" MouseLeftButtonDown="DataCDetails_MouseLeftButtonDown" Width="937">

并在后面的代码中提取了少量json数据,并将其添加为datagrid列,如下所示:

// Response converted to JSON
DataResponse dataResponse = JsonConvert.DeserializeObject<DataResponse>(response);
DataTable dt = new DataTable();
Dictionary<string, string> cols = new Dictionary<string, string>();

cols.Add("Column 1", "Col1");
cols.Add("Column 2", "Col2");
cols.Add("Column 3", "Col3");
cols.Add("Column 4", "Col4");
cols.Add("Column 5", "Col5");
cols.Add("Actions", "Actions");

foreach (var col in cols)
{
       var column = new DataGridTextColumn
       {
             Header = col.Key,
             Binding = new Binding(col.Value)
       };
       DataCDetails.Columns.Add(column);
}
foreach (var item in dataResponse.entry_list)
{
           var col1 = item.col1;
           var col2 = item.col2;
           var col3 = item.col3;
           var col4 = item.col4;
           var col5 = item.col5;
           DataCDetails.Items.Add(new ColumnItems { Col1 = col1, Col2 = col2, Col3 = col3, Col4 = col4, Col5 = col5});
}

我的ColumnItems课程如下:

public class ColumnItems
{
     public string Col1 { get; set; }
     public string Col2 { get; set; }
     public string Col3 { get; set; }
     public string Col4 { get; set; }
     public string Col5 { get; set; }
}

现在我如何将最后一列动态button添加到DataGrid中的每一行?我已尝试将Button数据属性添加到我的ColumnItems类,然后尝试将其添加为Action = new Button() DataCDetails.Items.Add,但效果不佳。希望有人能就此向我提供知识。

0 个答案:

没有答案