如何在wpf中具有特定模板的数据网格的C#代码中生成列

时间:2017-10-19 16:11:32

标签: c# wpf datagrid

我有这个代码并且工作正常,但我想在C#中动态更改它。任何人都可以帮我将这个XAML代码转换为C#代码吗?

注意:我在C#代码的pageload中设置了这个datagrid的Itemsource

注意:我希望在将此列转换为C#代码后,将AutoGenerateColumns更改为true

 <DataGrid x:Name="dg" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding ValueA}" />
                                    <TextBlock Text="{Binding ValueB}" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

1 个答案:

答案 0 :(得分:0)

AutoGenerateColumns = false:

public partial class MyPage:Page
{
    public MyPage()
    {
        InitializeDataGrid();
        Loaded += MyPage_Loaded;

        // don't forget to add the _dataGrid into the page
        this.Content = _dataGrid;
    }

    private void MyPage_Loaded(object sender, RoutedEventArgs e)
    {
        //_dataGrid.ItemsSource = xxx;
    }

    private void InitializeDataGrid()
    {
        // <DataGrid x:Name="dg" AutoGenerateColumns="False">
        //    <DataGrid.Columns>
        //        <DataGridTemplateColumn>
        //            <DataGridTemplateColumn.CellTemplate>
        //                <DataTemplate>
        //                    <StackPanel Orientation="Horizontal">
        //                        <TextBlock Text="{Binding ValueA}" />
        //                        <TextBlock Text="{Binding ValueB}" />
        //                    </StackPanel>
        //                </DataTemplate>
        //            </DataGridTemplateColumn.CellTemplate>
        //        </DataGridTemplateColumn>
        //    </DataGrid.Columns>
        //</DataGrid>

        // StackPanel and Children
        var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
        stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

        var textBlockFactoryA = new FrameworkElementFactory(typeof(TextBlock));
        textBlockFactoryA.SetBinding(TextBlock.TextProperty, new Binding("ValueA"));

        var textBlockFactoryB = new FrameworkElementFactory(typeof(TextBlock));
        textBlockFactoryB.SetBinding(TextBlock.TextProperty, new Binding("ValueB"));

        stackPanelFactory.AppendChild(textBlockFactoryA);
        stackPanelFactory.AppendChild(textBlockFactoryB);

        // DataTemplate
        var dataTemplate = new DataTemplate
        {
            VisualTree = stackPanelFactory
        };

        // DataGridTemplateColumn
        var templateColumn = new DataGridTemplateColumn
        {
            CellTemplate = dataTemplate
        };

        _dataGrid.Columns.Add(templateColumn);

        // DataGrid
        _dataGrid.Name = "gd";
        _dataGrid.AutoGenerateColumns = false;
    }

    private readonly DataGrid _dataGrid = new DataGrid();
}

AutoGenerateColumns = true:

public partial class MyPage:Page
{
    public MyPage()
    {
        InitializeDataGrid();
        Loaded += MyPage_Loaded;

        // don't forget to add the _dataGrid into the page
        this.Content = _dataGrid;
    }

    private void MyPage_Loaded(object sender, RoutedEventArgs e)
    {
        //_dataGrid.ItemsSource = xxx;
    }

    private void InitializeDataGrid()
    {
        // <DataGrid x:Name="dg" AutoGenerateColumns="False">
        //    <DataGrid.Columns>
        //        <DataGridTemplateColumn>
        //            <DataGridTemplateColumn.CellTemplate>
        //                <DataTemplate>
        //                    <StackPanel Orientation="Horizontal">
        //                        <TextBlock Text="{Binding ValueA}" />
        //                        <TextBlock Text="{Binding ValueB}" />
        //                    </StackPanel>
        //                </DataTemplate>
        //            </DataGridTemplateColumn.CellTemplate>
        //        </DataGridTemplateColumn>
        //    </DataGrid.Columns>
        //</DataGrid>

        //// StackPanel and Children
        //var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
        //stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

        //var textBlockFactoryA = new FrameworkElementFactory(typeof(TextBlock));
        //textBlockFactoryA.SetBinding(TextBlock.TextProperty, new Binding("ValueA"));

        //var textBlockFactoryB = new FrameworkElementFactory(typeof(TextBlock));
        //textBlockFactoryB.SetBinding(TextBlock.TextProperty, new Binding("ValueB"));

        //stackPanelFactory.AppendChild(textBlockFactoryA);
        //stackPanelFactory.AppendChild(textBlockFactoryB);

        // DataTemplate
        var dataTemplate = new DataTemplate
        {
            //VisualTree = stackPanelFactory
        };

        // DataGridTemplateColumn
        var templateColumn = new DataGridTemplateColumn
        {
            CellTemplate = dataTemplate
        };

        _dataGrid.Columns.Add(templateColumn);

        // DataGrid
        _dataGrid.Name = "gd";
        _dataGrid.AutoGenerateColumns = true;
    }

    private readonly DataGrid _dataGrid = new DataGrid();
}