我在代码中写道,如果我选择了行,那行背景颜色会发生变化。但是我有问题,如果我双击该行,背景再次改变颜色。不管我点击了多少,我必须得到相同的背景颜色。也许有人知道怎么做?
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(object sender, MouseButtonEventArgs e)
{
FrameworkElement originalSender = e.OriginalSource as FrameworkElement;
if (originalSender != null)
{
var row = originalSender.ParentOfType<GridViewRow>();
if (row != null)
{
row.Background = new SolidColorBrush(Colors.Red);
}
}
}
}
我也因为缺少ParentOfType()的命名空间而出错。我试试了Sytem.Web.UI.WebControls,但它显示它不存在
答案 0 :(得分:0)
请勿使用点击事件。您可以更改样式的背景颜色:
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
<Style.Triggers>
</Style>