Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使wpf DataGridRow选定颜色覆盖DataGridCell的绑定背景色?_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

C# 如何使wpf DataGridRow选定颜色覆盖DataGridCell的绑定背景色?

C# 如何使wpf DataGridRow选定颜色覆盖DataGridCell的绑定背景色?,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有一个DataGridTemplateColumn,它定义了一个具有绑定背景和前景属性的TextBlock。这允许颜色根据绑定属性的值进行更改。到目前为止还不错,只是我希望默认的选定行颜色覆盖绑定的背景颜色。如何在xaml中执行此操作 <DataGridTemplateColumn Header="Text"> <DataGridTemplateColumn.CellTemplate> <DataTemplate>

我有一个DataGridTemplateColumn,它定义了一个具有绑定背景和前景属性的TextBlock。这允许颜色根据绑定属性的值进行更改。到目前为止还不错,只是我希望默认的选定行颜色覆盖绑定的背景颜色。如何在xaml中执行此操作

<DataGridTemplateColumn Header="Text">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Text}"
                       Background="{Binding Path=BackgroundColor}"
                       Foreground="{Binding Path=ForegroundColor}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


最后,我似乎需要确定单元格是否在所选行中。如果是,请使用默认的选定行背景色,否则请使用绑定的背景色。我不知道该怎么办。任何帮助都将不胜感激。

您可以避免直接绑定
背景
,而是将a分配给
文本块
,该文本块在选择时使用a(
{binding IsSelected,RelativeSource={RelativeSource AncestorType=DataGridRow}
)设置为
false
,然后才将
Background
设置为绑定。

您可以避免直接绑定
Background
,而是将a分配给
TextBlock
,该文本块在选择中使用a(
{binding IsSelected,RelativeSource={RelativeSource AncestorType=DataGridRow}
)设置为
false
,然后才将
背景设置为绑定。

这不是最优雅的解决方案,但它相当简单

将“CurrentBackgroundColor”属性添加到项目中(需要实现属性更改),默认情况下,该属性设置为BackgroundColor。这就是你的背景

使用以下逻辑将双向SelectedItem绑定添加到DataGrid的属性:

public Item SelectedItem
{
  get
  {
    return selectedItem;
  }
  set
  {
    if (selectedItem != value)
    {
      if (selectedItem != null)
      {
        selectedItem.CurrentBackgroundColor = selectedItem.BackgroundColor;
      }

      selectedItem = value;

      if (selectedItem != null)
      {
        selectedItem.CurrentBackgroundColor = null;
      }

      RaisePropertyChanged("SelectedItem");
    }
  }
}
它的作用是

  • 如果存在当前选定的项目,请将其背景更改回其默认值
  • 已将selectedItem更新为新选择的项目
  • 通过将CurrentBackgroundColor设置为null清除它。这将允许显示选择突出显示

如果您想要一个更优雅的解决方案,我会研究EventTriggers

这不是最优雅的解决方案,但它相当简单

将“CurrentBackgroundColor”属性添加到项目中(需要实现属性更改),默认情况下,该属性设置为BackgroundColor。这就是你的背景

使用以下逻辑将双向SelectedItem绑定添加到DataGrid的属性:

public Item SelectedItem
{
  get
  {
    return selectedItem;
  }
  set
  {
    if (selectedItem != value)
    {
      if (selectedItem != null)
      {
        selectedItem.CurrentBackgroundColor = selectedItem.BackgroundColor;
      }

      selectedItem = value;

      if (selectedItem != null)
      {
        selectedItem.CurrentBackgroundColor = null;
      }

      RaisePropertyChanged("SelectedItem");
    }
  }
}
它的作用是

  • 如果存在当前选定的项目,请将其背景更改回其默认值
  • 已将selectedItem更新为新选择的项目
  • 通过将CurrentBackgroundColor设置为null清除它。这将允许显示选择突出显示

如果您想要一个更优雅的解决方案,我会研究EventTriggers,这是一个完美的解决方案。谢谢你看到我做不到的。这是一个完美的解决方案。谢谢你看到我不能看到的。谢谢你的意见。我一直在寻找xaml解决方案。谢谢你的意见。我沿着这条路走下去,但确实在寻找xaml解决方案。