【问题标题】:WPF double click change row background colorWPF双击更改行背景颜色
【发布时间】:2015-10-10 14:28:54
【问题描述】:

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

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,但它显示它不存在

【问题讨论】:

  • 请展示你的尝试!

标签: wpf colors background double-click


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多