gpt4 book ai didi

c# - WPF DataGrid 中选定行的前景色未更新

转载 作者:太空宇宙 更新时间:2023-11-03 10:54:15 28 4
gpt4 key购买 nike

我希望你们中的一些人可能遇到过这个问题,并且可以向我解释这里发生了什么。我已经尝试了一些修复(在 MVVM 世界中:)但没有帮助,我想在必要时应用一些 hack 之前了解这个问题,

我有一组三个数据网格列,它们使用前景色的值转换器 - 绿色表示正值,红色表示负值。

大部分时间都可以正常工作,但有时(真的不知道什么时候以及为什么)它不工作。在应用程序窗口未激活(用户看着屏幕但不与之交互)的情况下正确运行大约 20 分钟后,可能会发生这种情况。

一条重要的信息是,只要用户点击该行,颜色就会立即固定。

另外可能值得注意的是,整行颜色不会更新并且数字是正确的....

属性的所有更新都是通过调度程序上的 BeginInvoke 完成的,并且可能经常发生(大约每秒 20 次)。

Wrong colour from the foreground binding

欢迎任何想法。

(之前我认为这可能是虚拟化,但这并没有帮助)

<DataGrid
ItemsSource="{Binding Items}"
IsReadOnly="True"
AlternatingRowBackground="#E5E5E5"
EnableRowVirtualization="False"
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Standard">

数据网格列

<DataGridTextColumn Binding="{Binding A}" Header="A" Width="85" FontWeight="Bold">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource NotSelectableCellStyle}">
<Setter Property="Block.TextAlignment" Value="Right"/>
<Setter Property="Foreground" Value="{Binding A, Converter={StaticResource colorConverter}}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>

转换器

public class NegativePositiveColorConverter : IValueConverter
{
private static readonly Brush negativeBrush;

private static readonly Brush positiveBrush;

static NegativePositiveColorConverter()
{
negativeBrush = new SolidColorBrush(Colors.Red);
positiveBrush = new SolidColorBrush(Colors.Green);
negativeBrush.Freeze();
positiveBrush.Freeze();
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
decimal? decimalValue = value as decimal?;
if (!decimalValue.HasValue)
{
return Binding.DoNothing;
}

if (decimalValue < 0)
{
return negativeBrush;
}

return positiveBrush;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}

源对象属性

    public decimal? A
{
get
{
return this.a;
}

set
{
if (this.a!= value)
{
this.a= value;
OnPropertyChanged("A");
}
}
}

Cell Style(相当不重要)

<Style TargetType="DataGridCell" x:Key="NotSelectableCellStyle" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Background}"/>
</Trigger>
</Style.Triggers>
<Setter Property="Background" Value="{StaticResource EditableBrush}"/>
<Setter Property="Padding" Value="3,0,3,0" />
<Setter Property="Template" Value="{StaticResource NotSelectableDataGridCellTemplate}"/>
</Style>

<ControlTemplate x:Key="NotSelectableDataGridCellTemplate" TargetType="{x:Type DataGridCell}" >
<Grid>
<Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</Grid>
</ControlTemplate>

最佳答案

好的,我找到了问题,这要归功于(看起来很明显,但由于某些原因我之前没有遵循该路径)@Groo 的建议。我刚刚在转换器和属性 setter 中添加了很多日志记录,并注意到其中一行转换器永远不会执行,除非我单击网格表面。

这发生在代表所选项目的行(并且每次都是不同的行,具体取决于首先添加的行)。

因为我的样式不显示选择(请参阅我的问题的“单元格样式(相当不重要)”部分),所以我不会注意到失败行的任何特别之处。

我相信一种可能的修复方法是在任何项目被选中时将 SelectedItem 设置为 null。为什么选择项目时前景没有更新我不确定。

关于c# - WPF DataGrid 中选定行的前景色未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20095482/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com