【发布时间】:2020-09-06 12:37:19
【问题描述】:
我已经为数据库制作了一个工具,它可以显示具有某些过滤选项的项目。但是,在阅读了有关 WPF 和 C# 的更多信息之后。我使用这个https://www.codeproject.com/Articles/683429/Guide-to-WPF-DataGrid-formatting-using-bindings 教程来修改我的应用程序来处理ItemCollectionViewSource
我正在尝试将某些背景应用于不同的行。我已经创建了类:
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
namespace Liinos_inspector_FilterTest
{
class LiinosIDToBackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
{
string LiinosID = "";
if (LiinosID.Trim().StartsWith("7")) return Brushes.Blue;
if (LiinosID.Trim().StartsWith("6")) return Brushes.Yellow;
}
return Brushes.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
这是 XAML 部分:
<DataGrid Margin="0,146,0,0" Background="{x:Null}" BorderBrush="{x:Null}"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" IsReadOnly="True"
HorizontalGridLinesBrush="#FF377A6C" VerticalGridLinesBrush="#FF377A6C"
DataContext="{StaticResource ItemCollectionViewSource}"
ItemsSource="{Binding}"
AutoGenerateColumns="False" FontFamily="Arial Nova" Foreground="White" >
<!--RowBackground="Transparent"-->
<DataGrid.RowStyle>
<Style TargetType="DataGridRow" >
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Item.YRNRO, Converter={StaticResource LiinosIDToBackgroundConverter}}" />
</Style>
</DataGrid.RowStyle>
这是列的 XAML:
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding YRNRO}">
<DataGridTextColumn.Header>
<TextBlock Text="LIINOS ID" FontWeight="Bold" TextAlignment="Left"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding HAKUNIMI}">
<DataGridTextColumn.Header>
<TextBlock Text="SEARCH NAME" FontWeight="Bold" TextAlignment="Left"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
目前所有行都是红色的,因为看起来这不是真的if (value is string)。有人对如何解决这个问题有任何建议吗?
【问题讨论】:
-
您是否已经在调试模式下检查过什么是值传递?可以分享一下吗?