Conditional row colouring
Thank you for the project.
I'm developing a WinUI3 app.
I need to change row foreground / background according to item source property.
I find hard to understand how to set conditional properties since WinUI doesn't support style data triggers anymore.
I've tried using ItemContainerStyleSelector and I can change the row properties successfully but this won't be refreshed once the property is changed or the view is scrolled.
Do you have any advice regarding this kind of implementation?
Thank you in advance,
I saw it's in milestone now, I post the solution I'm currently using. Hope helps somehow.
Custom column:
public class CustomTextColumn : TableViewTextColumn
{
Binding? _fontWeight;
Binding? _fontColor;
public static readonly DependencyProperty MaxLenghtProperty = DependencyProperty.Register(nameof(MaxLength), typeof(int), typeof(CustomTextColumn), new(0));
public virtual Binding? ElementFontWeight
{
get
{
return _fontWeight;
}
set
{
_fontWeight = value;
if (_fontWeight is not null)
{
_fontWeight.Mode = BindingMode.TwoWay;
}
}
}
public virtual Binding? ElementFontColor
{
get
{
return _fontColor;
}
set
{
_fontColor = value;
if (_fontColor is not null)
{
_fontColor.Mode = BindingMode.TwoWay;
}
}
}
public int MaxLength
{
get
{
return (int)GetValue(MaxLenghtProperty);
}
set
{
SetValue(MaxLenghtProperty, value);
}
}
public override FrameworkElement GenerateElement(TableViewCell cell, object? dataItem)
{
TextBlock textBlock = new() { Margin = new Thickness(12.0, 0.0, 12.0, 0.0) };
textBlock.SetBinding(TextBlock.TextProperty, Binding);
if (ElementFontWeight != null)
{
textBlock.SetBinding(TextBlock.FontWeightProperty, ElementFontWeight);
}
if (ElementFontColor != null)
{
textBlock.SetBinding(TextBlock.ForegroundProperty, ElementFontColor);
}
return textBlock;
}
public override FrameworkElement GenerateEditingElement(TableViewCell cell, object? dataItem)
{
TextBox textBox = new() { MaxLength = MaxLength };
textBox.SetBinding(TextBox.TextProperty, Binding);
return textBox;
}
}
As resource:
<converters:BoolToObjectConverter x:Key="EditedRowFontWeight" FalseValue="Normal" TrueValue="Bold"/>
<converters:BoolToObjectConverter x:Key="DisabledFunctionConverter" FalseValue="{StaticResource GrayFont}" TrueValue="{StaticResource BlackFont}"/>
The table view:
<tableview:TableView x:Name="ModelsGrid">
<customTableView:CustomTextColumn Header="Function"
IsReadOnly="True"
Binding="{Binding SelectedType, Mode=OneWay}"
ElementFontWeight="{Binding IsChanged, Mode=OneWay, Converter={StaticResource EditedRowFontWeight}}"
ElementFontColor="{Binding ShouldRun, Mode=OneWay, Converter={StaticResource DisabledFunctionConverter}}"/>
</tableview:TableView>
Thanks for your patience and for sharing this idea, @fasigno! I really like it, but it only allows a few properties to change. In PR #253, I implemented a conditional cell styling feature that allows to change the entire style of a cell based on a condition. I hope you like it, and feel free to share any questions or suggestions to improve this functionality.
Thanks a lot for your solution, definitely better. Can I apply read only property on specific column according to a binding with the new cell style?