我正在尝试在代码堆中更改行datagrid的背景颜色我正在使用绑定属性 在代码behing中我看到颜色变化,但在显示时,背景颜色不会改变。 当我使用滚动播放时我会看到一些
答案 0 :(得分:0)
标题颜色更改:
With datagrid1
.ColumnHeadersDefaultCellStyle.Font = New Font("Arial", 12.0F, FontStyle.Bold, GraphicsUnit.Pixel)
.ColumnHeadersDefaultCellStyle.BackColor = Color.GreenYellow
.EnableHeadersVisualStyles = False
End With
行和背景颜色变化
With datagrid1
.RowsDefaultCellStyle.BackColor = Color.Beige
.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(224, 255, 179)
.DefaultCellStyle.Font = New Font("Arial", 12.0F, FontStyle.Regular, GraphicsUnit.Pixel)
End With
这样改变。我希望对你有所帮助
答案 1 :(得分:0)
完整行:
datagrid1.Rows(0).DefaultCellStyle.BackColor = Color.Beige
完整专栏:
datagrid1.Columns(0).DefaultCellStyle.BackColor = Color.Yellow
使用条件和特定单元格
If (dataset1.Tables(0).Rows(1)(0) == "") Then
datagrid1.Rows(0).Cells(1).Style.BackColor = Color.Red
End if
答案 2 :(得分:0)
我创建了一个示例CustomClass:
public class CustomClass
{
public string PropertyToBeWatched { get; set; }
}
创建了一个列表:
MyList = new ObservableCollection<CustomClass>();
MyList.Add(new CustomClass() { PropertyToBeWatched = "1"});
MyList.Add(new CustomClass() { PropertyToBeWatched = "2" });
MyList.Add(new CustomClass() { PropertyToBeWatched = "2" });
MyList.Add(new CustomClass() { PropertyToBeWatched = "2" });
然后创建了Datagrid:
<sdk:DataGrid ItemsSource="{Binding MyList}" RowStyle="{StaticResource Style1}">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding PropertyToBeWatched}" Header="Property1"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
然后是资源:
xmlns:sdk1="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
xmlns:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
<SolidColorBrush x:Key="Red" Color="#FFFF0000" />
<SolidColorBrush x:Key="Green" Color="#FF00FF00" />
<Style x:Key="Style1" TargetType="sdk:DataGridRow">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:DataGridRow">
<Grid Background="{Binding Converter={StaticResource Test}}">
<sdk1:DataGridFrozenGrid x:Name="Root">
<sdk1:DataGridFrozenGrid.Resources>
<Storyboard x:Key="DetailsVisibleTransition">
<DoubleAnimation Duration="00:00:0.1" Storyboard.TargetProperty="ContentHeight" Storyboard.TargetName="DetailsPresenter"/>
</Storyboard>
</sdk1:DataGridFrozenGrid.Resources>
<sdk1:DataGridFrozenGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</sdk1:DataGridFrozenGrid.ColumnDefinitions>
<sdk1:DataGridFrozenGrid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</sdk1:DataGridFrozenGrid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="NormalAlternatingRow">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="NormalSelected">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOverSelected">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="UnfocusedSelected">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="UnfocusedEditing"/>
<VisualState x:Name="NormalEditing"/>
<VisualState x:Name="MouseOverUnfocusedEditing"/>
<VisualState x:Name="MouseOverEditing"/>
<VisualState x:Name="MouseOverUnfocusedSelected"/>
</VisualStateGroup>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid"/>
<VisualState x:Name="Invalid">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="BackgroundRectangle">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="InvalidVisualElement"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="BackgroundRectangle" Grid.ColumnSpan="2" Fill="Red" Opacity="0" Grid.RowSpan="2"/>
<Rectangle x:Name="InvalidVisualElement" Grid.ColumnSpan="2" Fill="#FFF7D8DB" Opacity="0" Grid.RowSpan="2"/>
<sdk1:DataGridRowHeader x:Name="RowHeader" sdk1:DataGridFrozenGrid.IsFrozen="True" Grid.RowSpan="3"/>
<sdk1:DataGridCellsPresenter x:Name="CellsPresenter" Grid.Column="1" sdk1:DataGridFrozenGrid.IsFrozen="True"/>
<sdk1:DataGridDetailsPresenter x:Name="DetailsPresenter" Grid.Column="1" Grid.Row="1"/>
<Rectangle x:Name="BottomGridLine" Grid.Column="1" HorizontalAlignment="Stretch" Height="1" Grid.Row="2"/>
</sdk1:DataGridFrozenGrid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是转换器:
public class RowStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (((CustomClass)value).PropertyToBeWatched == "1")
return App.Current.Resources["Red"] as SolidColorBrush;
else
return App.Current.Resources["Green"] as SolidColorBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
它基本上根据属性“PropertyToBeWatched”获取并设置solidcolorbrush。
希望这有帮助