我有一個datagrid,其中的行可能有一個狀態,其顏色代碼或數據要求它們加粗。我已經能夠輕鬆地使膽量工作。如果它具有顏色代碼,我希望整個行背景以該顏色顯示。我搜索了幾個網站,並嘗試了許多不同的方式來獲得行顏色編碼,但它們都不起作用。我錯過了什麼?
Datagrid.xaml(風格):WPF DataGrid行着色
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="#FFFFFF" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
<Setter Property="ValidationErrorTemplate">
<Setter.Value>
<ControlTemplate>
<TextBlock Margin="2,0,0,0"
VerticalAlignment="Center"
Foreground="Red"
Text="!" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Border x:Name="DGR_Border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#AAFFFF" />
<GradientStop Offset="1" Color="#AAFFD4" />
</LinearGradientBrush>
</Border.Background>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFFFF" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFFFF" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Normal_AlternatingRow">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFFFF" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#E6F7FF" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Normal_Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#2e90d1" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#2980b9" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFFE6" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFF80" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</SelectiveScrollingGrid.RowDefinitions>
<DataGridCellsPresenter Grid.Column="1"
ItemsPanel="{TemplateBinding ItemsPanel}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<DataGridDetailsPresenter Grid.Row="1"
Grid.Column="1"
SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen,
ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical},
Converter={x:Static DataGrid.RowDetailsScrollingConverter},
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
Visibility="{TemplateBinding DetailsVisibility}" />
<DataGridRowHeader Grid.RowSpan="2"
SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
Visibility="{Binding HeadersVisibility,
ConverterParameter={x:Static DataGridHeadersVisibility.Row},
Converter={x:Static DataGrid.HeadersVisibilityConverter},
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
</SelectiveScrollingGrid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="border"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="1"
SnapsToDevicePixels="True">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Unfocused" />
<VisualState x:Name="Focused" />
</VisualStateGroup>
<VisualStateGroup x:Name="CurrentStates">
<VisualState x:Name="Regular" />
<VisualState x:Name="Current">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="Gray" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ListManager.xaml(形式):
<DataGrid
SelectedItem="{Binding SelectedLead, Mode=TwoWay}"
ItemsSource="{Binding LeadListFiltered}"
x:Name="grdWorkLeadList"
AutoGenerateColumns="False"
Margin="-10,8,2,0"
IsReadOnly="True"
AlternationCount="2"
SelectionChanged="grdWorkLeadList_SelectionChanged"
LoadingRow="grdWorkLeadList_LoadingRow">
<DataGrid.Columns>
<DataGridTextColumn Header="Campaign" Binding="{Binding SetupCampaign.Name}" />
<DataGridTextColumn Header="Action Status" Binding="{Binding ActionStatus}" />
<DataGridTextColumn Header="Action Date" Binding="{Binding ActionDate,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTemplateColumn Header="Name" SortMemberPath="Lastname">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Title }" />
<Run Text=" " />
<Run Text="{Binding Firstname }" />
<Run Text=" " />
<Run Text="{Binding Lastname }" />
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Product" Binding="{Binding Product }" />
<DataGridTextColumn Header="Sailing" Binding="{Binding Sailing,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Stateroom" Binding="{Binding Stateroom }" />
<DataGridTextColumn Header="Booking Date" Binding="{Binding BookingDate,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Cancel Date" Binding="{Binding CancelDate,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Disposition" Binding="{Binding DispositionValue}" />
<DataGridTextColumn Header="Booking" Binding="{Binding Booking }" />
<DataGridTextColumn Header="Run Date" Binding="{Binding RunDate ,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Phone" Binding="{Binding Phone }" />
<DataGridTextColumn Header="Followup" Binding="{Binding ActionFollowup}" />
<DataGridTextColumn Header="ID" Binding="{Binding ID }" />
<DataGridTextColumn Header="Assigned Agent" Binding="{Binding AssignedAgent }" />
<DataGridTextColumn Header="Active" Binding="{Binding ActiveStatus }" />
<DataGridTextColumn Header="Disposition Date" Binding="{Binding DispositionDate ,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Rebook #" Binding="{Binding ReBookNumber }" />
<DataGridTextColumn Header="Close Date" Binding="{Binding CloseDate ,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Reason" Binding="{Binding ReasonDescription}" />
<DataGridTextColumn Header="Code" Binding="{Binding Code }" />
</DataGrid.Columns>
</DataGrid>
代碼隱藏(CS):
private void grdWorkLeadList_LoadingRow(object sender, DataGridRowEventArgs e)
{
Lead item = e.Row.Item as Lead;
string color;
if (item != null)
{
//handle status colors
if (item.CurrentDisposition != null)
using (var db = new DAOutboundCM_DEVEntities())
{
color = (from d in db.SetupDispositionLists where d.ID == item.CurrentDisposition select d.Color).Single();
e.Row.Background = new SolidColorBrush((Color) ColorConverter.ConvertFromString(color));
e.Row.UpdateLayout();
}
//handle priority bolding
if (item.SetupCampaign.Name.Contains("Quotes"))
if (item.Phone != null && item.Phone != "")
e.Row.FontWeight = FontWeights.Bold;
}
}
您發佈了太多的代碼,可能會使想要回答您的問題的人感到困惑。 請嘗試簡要解釋您的問題。 – ViVi