以编程方式设置Datagrid(WPF)列样式(不是xaml) [英] Datagrid (WPF) Column styling programmatically (not xaml)
问题描述
我看过SO,但找不到我想要的确切答案。我有一个绑定到数据源的DataGrid视图。我想在带有datagrid的窗口可见后以编程方式设置列的样式。我还想根据某些行为不时更改它。
我试图使用DataGridTemplateColumn,但是只要它运行,它就会从那些列中删除数据。另外,当我尝试从资源中获取单元格样式时(即始终为空),我也没有得到单元格样式。
private void StyleColumns ()
{
//用自定义模板列替换DueDate列。
for(int i = 0; i< 7; i + = 2)
{
//创建一个新的模板列。
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = m_DataGrid.Columns [i] .Header;
样式样式= new Style();
templateColumn.CellStyle =(Style)Resources [ ColumnGone];
// ...
//用templateColumn替换自动生成的列。
m_DataGrid.Columns [i] = templateColumn;
}
}
XAML就是这样
< DataGrid AutoGenerateColumns = True x:Name = m_grfFileDataGrid ItemsSource = {Binding cb.GRF}
RowHeight = 20 ColumnWidth = *
AlternatingRowBackground =米色
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>
任何对此的帮助将不胜感激。谢谢
下面是添加带有 Style
的列的示例:
XAML
< Grid>
< DataGrid x:Name = m_DataGrid Width = 400
AutoGenerateColumns = True
HorizontalAlignment = Left
RowHeight = 20 ColumnWidth = *
AlternatingRowBackground =米色
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>
<按钮名称= AddColumn内容= AddColumn宽度= 100高度= 30 HorizontalAlignment = Right Click = AddColumn_Click />
< / Grid>
后面的代码
公共类Person
{
公共字符串样本
{
;
组;
}
}
私人ObservableCollection< Person> TestCollection =新的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 =测试;
MyColumn.Binding = new Binding( Sample);
样式样式=(样式)m_DataGrid.Resources [ ColumnGone];
MyColumn.CellStyle =样式;
m_DataGrid.Columns.Add(MyColumn);
}
private void AddColumn_Click(object sender,RoutedEventArgs e)
{
StyleColumns();
}
最有可能的是,您没有指出绑定
或者,为现有列设置 Style
p>
指定列的名称:
< DataGridTextColumn x:Name = MySuperColumn标头= MyColumn Binding = {绑定路径=样本} Width = 100 />
在代码中设置 Style
MySuperColumn.CellStyle = style;
I have looked on SO but haven't found an exact answer to what I am looking for. I have a DataGrid view bound to a data source. I want to style columns programmatically after the window with the datagrid is visible. I also want to change it from time to time based on some behavior.
I tried to use the DataGridTemplateColumn but whenever it runs it deletes the data from those columns. Also I don't get the Cell Style when I try to get it from Resources (i.e. its always 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 is like this
<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>
Any help on this would be appreciated. thanks
Here's an example of adding a column with 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();
}
Most likely, you do not pointed Binding
for the new column.
Alternatively, set the Style
for an existing column:
Specify the name of the column:
<DataGridTextColumn x:Name="MySuperColumn" Header="MyColumn" Binding="{Binding Path=Sample}" Width="100" />
Set the Style
in code:
MySuperColumn.CellStyle = style;
这篇关于以编程方式设置Datagrid(WPF)列样式(不是xaml)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!