【问题标题】:Unable to add a column which contains a Button to a dynamic DataGrid无法将包含按钮的列添加到动态 DataGrid
【发布时间】:2011-07-12 14:35:25
【问题描述】:

我正在开发一个基于返回数据集中的表创建动态数据网格的应用程序。
所有表可能有不同的列,所以我根据行数创建动态数据网格并设置 AutoGenerateColumns = true;
这是我的 C# 代码:

 for (int count = 0; count < ds.Tables.Count; count++)
            {
                DataGrid dg = new DataGrid();
                dg.Name = ds.Tables[count].TableName.ToString();
                dg.Margin = new Thickness(5);
                dg.Width = 800;
                dg.MaxHeight = 200;
                dg.AutoGenerateColumns = true;
                dg.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                dg.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                dg.ItemsSource = ds.Tables[count].DefaultView;
                stkCollection.Children.Add(dg);
            }

我想在每行都有一个按钮的每个动态数据网格中添加一个额外的列。

这是我的 XAML:

<GroupBox Header="Log Details">

    <Border  >
        <Grid Background="{StaticResource NormalBackground}">
            <Grid.ColumnDefinitions >
                <ColumnDefinition Width="150" />
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="150"/>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="150"/>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="150"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="10"/>
                <RowDefinition Height="60"/>
                <RowDefinition Height="10"/>
                <RowDefinition Height="*" MaxHeight="600"/>
            </Grid.RowDefinitions>
            <Button Name="btnMessage" Content="Message" Grid.Row="1" Grid.Column="2" Width="120" Height="50" HorizontalAlignment="right" Click="btnMessage_Click"></Button>
            <Button Name="btnDraw" Content="Draw" Grid.Row="1" Grid.Column="4" Width="120" Height="50" HorizontalAlignment="right" Click="btnDraw_Click"></Button>
            <StackPanel x:Name="stkCollection"  Grid.Row="3"  Grid.Column="0" Grid.ColumnSpan="8" Orientation="Vertical"/>


        </Grid>

    </Border>

</GroupBox>

任何人都有任何想法。
我是怎么做到的?
提前致谢

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    感谢您的快速回复.. 我找到了解决方案... 这是我的代码...

    for (int count = 0; count < ds.Tables.Count; count++)
                {
                    DataGrid dg = new DataGrid();
                    dg.Name = ds.Tables[count].TableName.ToString();
                    dg.Margin = new Thickness(5);
                    dg.Width = 800;
                    dg.MaxHeight = 200;
                    DataGridTemplateColumn dgc = new DataGridTemplateColumn();
                    DataTemplate dtm = new DataTemplate();
    
                    FrameworkElementFactory btnReset = new FrameworkElementFactory(typeof(Button));
                    btnReset.SetValue(Button.ContentProperty, "Restore");
                    btnReset.SetValue(Button.ToolTipProperty, "Restore Selected Row");
                    btnReset.SetValue(Button.DataContextProperty, new Binding("TableName"));
    
                    btnReset.AddHandler(Button.ClickEvent, new RoutedEventHandler(btn_Click));
    
                    //set the visual tree of the data template  
                    dtm.VisualTree = btnReset; 
                    dgc.CellTemplate = dtm;
                    dg.Columns.Add(dgc);
                    dg.AutoGenerateColumns = true;
                    dg.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                    dg.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                    dg.ItemsSource = ds.Tables[count].DefaultView;
                    stkCollection.Children.Add(dg);
                }
    

    谢谢..

    【讨论】:

    • @user649985:这不是答案。您可以通过编辑或评论您自己的问题或其他人的答案来更新您的原始问题。
    【解决方案2】:

    您可以像这样在 Xaml 中将 DataGridTemplateColumn 定义为资源:

    <Window.Resources>
        <DataGridTemplateColumn x:Key="DGTemplateColumn">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Content="Info" Click="DGCell_Button_Click"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </Window.Resources>
    

    在代码中将此添加到您的 DataGrid:

    DataGrid.Columns.Add(FindResource("DGTemplateColumn") as DataGridTemplateColumn);
    

    并处理按钮的单击事件(这里我使用按钮的 DataContext 显示员工的姓名,这是该行中显示的基础对象):

    private void DGCell_Button_Click(object sender, RoutedEventArgs e)
    {
        Employee emp = (sender as Button).DataContext as Employee;
        MessageBox.Show(emp.Name);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      相关资源
      最近更新 更多