I want to make a custom filter for my TreeDataGrid
(flat). My model LogLine have reactive property IsVisible
, but all my attempts to add binding to model from TreeDataGridRow
style are useless.
<TreeDataGrid.Styles>
<Style Selector="TreeDataGridRow"
x:DataType="model:LogLine">
<Setter Property="IsVisible" Value="{Binding $self.((model:LogLine)DataContext).IsVisible, Mode=TwoWay}"/>
</Style>
</TreeDataGrid.Styles>
P.S. Inspector showing correct DataContext in TreeDataGridRow...
NET 8 Avalonia 11.1.3 (latest) TreeDataGrid 11.0.10 (latest)
I'm trying bindings, template bindings, but it dosn't work
You will not be able to set TreeDataGridRow.IsVisible
from Selector
, because (as of now) it is set to true
with LocalValue
priority. In the future pay attention to "Priority" column in Avalonia inspector.
You can set the value with the help of attached behavior.
Here is a solution with behavior:
using Avalonia;
using Avalonia.Controls.Primitives;
namespace Behaviors;
public class TreeDataGridBehavior : AvaloniaObject
{
public static readonly AttachedProperty<bool> IsRowVisibleProperty =
AvaloniaProperty.RegisterAttached<TreeDataGridBehavior, bool>(
"IsRowVisible", typeof(TreeDataGridBehavior));
public static void SetIsRowVisible(AvaloniaObject element, bool value) =>
element.SetValue(IsRowVisibleProperty, value);
public static bool GetIsRowVisible(AvaloniaObject element) =>
element.GetValue(IsRowVisibleProperty);
static TreeDataGridBehavior()
{
IsRowVisibleProperty.Changed.AddClassHandler<TreeDataGridRow, bool>(
(row, e) => row.IsVisible = e.NewValue == true);
}
}
And xaml (define on root element xmlns:bb="clr-namespace:Behaviors"
):
<TreeDataGrid.Styles>
<Style Selector="TreeDataGridRow">
<Setter Property="bb:TreeDataGridBehavior.IsRowVisible"
Value="{ReflectionBinding IsVisible}"/>
</Style>
</TreeDataGrid.Styles>