WPF双击更改行背景色

WPF双击更改行背景色,wpf,colors,background,double-click,Wpf,Colors,Background,Double Click,我在代码中写道,如果我选择了行,则该行的背景颜色会改变。但我有问题,如果我双击该行,背景再次改变颜色。无论我点击多少次,我都必须得到相同的背景色。也许有人知道怎么做 XAML: <DataGrid x:Name="lbPersonList" VerticalScrollBarVisibility="Visible" AlternationCount="2" AutoGenerateColumns="False" GridLinesVisibility="Non

我在代码中写道,如果我选择了行,则该行的背景颜色会改变。但我有问题,如果我双击该行,背景再次改变颜色。无论我点击多少次,我都必须得到相同的背景色。也许有人知道怎么做

XAML:

<DataGrid x:Name="lbPersonList" VerticalScrollBarVisibility="Visible" AlternationCount="2" AutoGenerateColumns="False" 
              GridLinesVisibility="None" CanUserAddRows="False" SelectionMode="Single" 
              HeadersVisibility="Column" ScrollViewer.CanContentScroll="False" MouseDoubleClick="lbPersonList_MouseDoubleClick_1">

.cs:

private void lbPersonList_MouseDoubleClick_1(对象发送器,MouseButtonEventArgs e)
{
FrameworkElement originalSender=e.OriginalSource作为FrameworkElement;
if(originalSender!=null)
{
var row=originalSender.ParentOfType();
如果(行!=null)
{
row.Background=新的SolidColorBrush(Colors.Red);
}
}
}
}

我还发现ParentOfType()缺少命名空间的错误。我尝试使用Sytem.Web.UI.WebControls,但它显示它不存在

不要使用单击事件。您可以在样式中更改背景色:

<Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
      <Trigger Property="IsSelected" Value="True">
        <Setter Property="Background" Value="Red"/>
      </Trigger>
    <Style.Triggers>
</Style>

请展示您的尝试!
<Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
      <Trigger Property="IsSelected" Value="True">
        <Setter Property="Background" Value="Red"/>
      </Trigger>
    <Style.Triggers>
</Style>