gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:47:53 27 4
gpt4 key购买 nike

我看过 SO 但没有找到我正在寻找的确切答案。我有一个绑定(bind)到数据源的 DataGrid View 。我想在带有数据网格的窗口可见后以编程方式设置列的样式。我还想根据某些行为不时更改它。

我尝试使用 DataGridTemplateColumn,但每当它运行时,它都会从这些列中删除数据。当我尝试从资源中获取单元格样式时,我也没有得到它(即它始终为空)

        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>

如有任何帮助,我们将不胜感激。谢谢

最佳答案

下面是添加具有样式的列的示例:

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>

代码隐藏

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

或者,为现有列设置样式:

指定列名:

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

在代码中设置样式:

MySuperColumn.CellStyle = style;

关于wpf - Datagrid (WPF) 以编程方式设置列样式(不是 xaml),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17911159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com