Wpf 更改datagrid单元的背景
我可以通过下面的C代码更改datacell的背景-Wpf 更改datagrid单元的背景,wpf,datagrid,Wpf,Datagrid,我可以通过下面的C代码更改datacell的背景- private void检索_行(对象项) { DataRow row=mygrid.GetContainerFromItem(item)作为DataGrid.DataRow; 如果(行!=null) { SolidColorBrush redColor=新的SolidColorBrush(Colors.Red); foreach(行中的DataGrid.DataCell单元格) { var dc=((System.Windows.Framew
private void检索_行(对象项)
{
DataRow row=mygrid.GetContainerFromItem(item)作为DataGrid.DataRow;
如果(行!=null)
{
SolidColorBrush redColor=新的SolidColorBrush(Colors.Red);
foreach(行中的DataGrid.DataCell单元格)
{
var dc=((System.Windows.FrameworkElement)((DataGrid.Cell)(Cell)).ParentRow)).DataContext;
//如果IsBlank值设置为true,则获取自定义对象并更改颜色
MyRowObject rowObject=dc作为MyRowObject;
对于(int counter=0;计数器
但是有了这段代码,应用程序的性能会大大降低。是否有任何方法将上述代码转换为XAML触发器/或任何其他方法来提高网格的性能。欢迎来到WPF世界;)
你可以试试这个:
<DataGrid Name="myGrid">
<DataGrid.Columns>
<DataGridTextColumn Header="Col1" Binding="{Binding Col1}" />
<DataGridTextColumn Header="Col2" Binding="{Binding Col2}" />
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Red" />
</Style>
</DataGrid.CellStyle>
</DataGrid>
干杯
Sebi欢迎来到WPF世界;)
你可以试试这个:
<DataGrid Name="myGrid">
<DataGrid.Columns>
<DataGridTextColumn Header="Col1" Binding="{Binding Col1}" />
<DataGridTextColumn Header="Col2" Binding="{Binding Col2}" />
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Red" />
</Style>
</DataGrid.CellStyle>
</DataGrid>
干杯
Sebi我认为您不能,因为datagrid只定义了结构而不是样式。
我在网格单元中使用矩形
<Rectangle Grid.Column="1" Grid.Row="1" Fill="Red"></Rectangle>
<TextBox Grid.Column="1" Grid.Row="1" Background="Transparent" Text="test"></TextBox>
我认为您不能,因为datagrid只定义结构而不是样式。
我在网格单元中使用矩形
<Rectangle Grid.Column="1" Grid.Row="1" Fill="Red"></Rectangle>
<TextBox Grid.Column="1" Grid.Row="1" Background="Transparent" Text="test"></TextBox>
由于需要两个动态值来确定单元格的背景颜色(
ColumnIndex
和ValuesList
),因此需要使用一个多转换器
,它接受这两个值并返回颜色
比如说,
if ValueList[ColumnIndex].IsBlank)
Return Colors.Red; // Might be Brushes.Red too, can't remember
else
Return Colors.White;
然后,触发器可以隐式地应用于所有DataGridCells
,其样式没有指定键
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource MyMultiConverter}">
<Binding Path="Column.DisplayIndex" RelativeSource="{RelativeSource Self}" />
<Binding Path="ValueList" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
我可能在
列中的RelativeSource
语法错误。多重绑定的DisplayIndex
绑定,但是绑定应该指向Self
,这是DataGridCell
,因为需要两个动态值来确定单元格的背景颜色(ColumnIndex
和ValuesList
),您需要使用一个多转换器
,它接受这两个值并返回颜色
比如说,
if ValueList[ColumnIndex].IsBlank)
Return Colors.Red; // Might be Brushes.Red too, can't remember
else
Return Colors.White;
然后,触发器可以隐式地应用于所有DataGridCells
,其样式没有指定键
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource MyMultiConverter}">
<Binding Path="Column.DisplayIndex" RelativeSource="{RelativeSource Self}" />
<Binding Path="ValueList" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
我可能在列中的RelativeSource
语法错误。DisplayIndex
绑定的多绑定
,但是绑定应该指向Self
,这是DataGridCell,谢谢您的回复。但是我已经尝试了遵循xaml代码,但不起作用。请注意更具体一点-你打算用这个multidatatrigger做什么?这似乎有点离题了…嗨,Sebi,谢谢你的回答。但我已经尝试了以下xaml代码,但它不起作用。请更具体一点-你打算用这个multidatatrigger做什么?这似乎有点离题了。。。