Datagrid(WPF)以编程方式设置列样式(不是xaml)

时间:2013-07-28 18:02:24

标签: wpf datagrid wpfdatagrid datagridtemplatecolumn

我已经看过了,但没有找到我正在寻找的确切答案。我有一个绑定到数据源的DataGrid视图。我希望在具有datagrid的窗口可见后以编程方式设置列的样式。我还想根据某些行为不时改变它。

我尝试使用DataGridTemplateColumn但是无论何时运行它都会删除这些列中的数据。当我尝试从Resources获取它时,我也没有得到Cell Style(即它总是为null)

        private void StyleColumns()
    {
        // Replace the DueDate column with a custom template column.
        for (int i = 0; i < 7; i += 2)
        {
            // Create a new template column.
            DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
            templateColumn.Header = m_DataGrid.Columns[i].Header;
            Style style = new Style();
            templateColumn.CellStyle = (Style)Resources["ColumnGone"];
            // ...
            // Replace the auto-generated column with the templateColumn.
            m_DataGrid.Columns[i] = templateColumn;
        }
    }

XAML就像这样

                        <DataGrid AutoGenerateColumns="True" x:Name="m_grfFileDataGrid" ItemsSource="{Binding cb.GRF}"
                              RowHeight="20" ColumnWidth="*"
                              AlternatingRowBackground="Beige"
                              SelectionUnit="CellOrRowHeader"
                              FontFamily="Consolas"
                              FontSize="12"
                              CanUserReorderColumns="False"
                              CanUserSortColumns="False"
                              CanUserAddRows="False"
                              CanUserDeleteRows="False">
                        <DataGrid.Resources>
                            <Style TargetType="DataGridCell" x:Key="ColumnGone">
                                <Setter Property="Background" Value="SeaGreen"/>
                            </Style>
                            <Style x:Key="DisabledColumn">
                                <Setter Property="DataGridColumn.CanUserResize"
                                        Value="False" />
                                <Setter Property="DataGridColumn.CanUserSort"
                                        Value="False" />
                                <Setter Property="DataGridColumn.CanUserReorder"
                                        Value="False" />
                                <Setter Property="DataGridColumn.CellStyle"
                                        Value="{StaticResource ColumnGone}" />
                            </Style>
                        </DataGrid.Resources>
                    </DataGrid>

对此有任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:3)

以下是使用Style添加列的示例:

XAML

<Grid>
    <DataGrid x:Name="m_DataGrid" Width="400" 
                          AutoGenerateColumns="True"
                          HorizontalAlignment="Left"
                          RowHeight="20" ColumnWidth="*"
                          AlternatingRowBackground="Beige"
                          SelectionUnit="CellOrRowHeader"
                          FontFamily="Consolas"
                          FontSize="12"
                          CanUserReorderColumns="False"
                          CanUserSortColumns="False"
                          CanUserAddRows="False"
                          CanUserDeleteRows="False">

        <DataGrid.Resources>
            <Style TargetType="DataGridCell" x:Key="ColumnGone">
                <Setter Property="Background" Value="SeaGreen" />
                <Setter Property="Foreground" Value="White" />
            </Style>

            <Style x:Key="DisabledColumn">
                <Setter Property="DataGridColumn.CanUserResize"
                                    Value="False" />
                <Setter Property="DataGridColumn.CanUserSort"
                                    Value="False" />
                <Setter Property="DataGridColumn.CanUserReorder"
                                    Value="False" />
                <Setter Property="DataGridColumn.CellStyle"
                                    Value="{StaticResource ColumnGone}" />
            </Style>
        </DataGrid.Resources>
    </DataGrid>

    <Button Name="AddColumn" Content="AddColumn" Width="100" Height="30" HorizontalAlignment="Right" Click="AddColumn_Click" />
</Grid>

Code behind

public class Person
{
    public string Sample
    {
        get;
        set;
    }
}

private ObservableCollection<Person> TestCollection = new ObservableCollection<Person>();

public MainWindow()
{
    InitializeComponent();

    TestCollection.Add(new Person()
    {
        Sample = "Orange",
    });

    TestCollection.Add(new Person()
    {
        Sample = "White",
    });

    TestCollection.Add(new Person()
    {
        Sample = "Green",
    });

    m_DataGrid.ItemsSource = TestCollection;
}

private void StyleColumns()
{            
    DataGridTextColumn MyColumn = new DataGridTextColumn();
    MyColumn.Header = "Test";
    MyColumn.Binding = new Binding("Sample");

    Style style = (Style)m_DataGrid.Resources["ColumnGone"];
    MyColumn.CellStyle = style;           
    m_DataGrid.Columns.Add(MyColumn);
}

private void AddColumn_Click(object sender, RoutedEventArgs e)
{
    StyleColumns();
}

最有可能的是,您没有将Binding指向新列。

或者,为现有列设置Style

指定列的名称:

<DataGridTextColumn x:Name="MySuperColumn" Header="MyColumn" Binding="{Binding Path=Sample}" Width="100" />

在代码中设置Style

MySuperColumn.CellStyle = style;