1
我正在編寫一個快速而髒的WPF應用程序作爲完整的WPF新手。我有一個簡單的數據網格:突出顯示WPF數據網格中的單元格時不顯示任何文本
<DataGrid ItemsSource="{Binding Path=BetterFoods}" Grid.Row="1" Grid.Column="0" AutoGenerateColumns="True" Loaded="DataGrid_Loaded">
<DataGrid.Resources>
<local:ValueColorConverter x:Key="colorconverter"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF0033"/>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource colorconverter}}"/>
</Style>
</DataGrid.CellStyle>
</DataGrid>
數據網格是由一個簡單的IValueConverter
,這幾乎是相同的教程和堆棧溢出的例子負荷有色:
class ValueColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return Brushes.Beige;
}
else if (value is string)
{
string str = ((string)value).Trim();
if (str.Equals(string.Empty))
{
return Brushes.Beige;
}
else if (str.Equals("0"))
{
return Brushes.LightYellow;
}
}
return System.Windows.DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
的IValueConverter
工作完全像它應該,但它引入了一個奇怪的副作用:選擇一行使得其單元格中的所有值都消失。這些值仍然存在,因爲更改選擇或雙擊單元格會使它們再次可見(請參閱下面的.gif文件)。
這顯然是不幸的,因爲人們通常突出了行,從而具有在其數據仔細看看。
什麼是造成這種行爲,我該如何解決它?