Reputation: 2402
I have made a tool for database that is showing items with certain filtering options. However after reading some more about WPF and C#. I used this https://www.codeproject.com/Articles/683429/Guide-to-WPF-DataGrid-formatting-using-bindings tutorial to modify my application to deal with ItemCollectionViewSource
I am trying to apply certain background to different rows. I have created Class:
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();
}
}
}
Here is XAML part:
<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>
Here is XAML for columns:
<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>
Currently all rows are red as it looks like this is not true if (value is string)
. Do anyone have any suggestions how this can be fixed?
Upvotes: 0
Views: 44
Reputation: 12276
The problem, or one of them anyhow, is your binding. When you do:
Value="{Binding RelativeSource={RelativeSource Self}, Path=Item.YRNRO,
It's looking for a property Item on the DataGridRow which has a property YRNRO.
It is more likely you will want to bind to something which is the datacontext of the datagridrow which means you should remove the relativesource binding.
You have a collection of something which is the collectionviewsource. Each row in the datagrid gets a something as datacontext.
Depending on exactly what that is you should use:
<Setter Property="Background" Value="{Binding YRNRO, Converter={StaticResource LiinosIDToBackgroundConverter}}" />
Or if something has a property called Item which is a complex object with a proprty YNRNO then
<Setter Property="Background" Value="{Binding Item.YRNRO, Converter={StaticResource LiinosIDToBackgroundConverter}}" />
Upvotes: 1