Data validation error text appears inside TemplatedControl, but inner TextBox does not get a border

1 day ago 3
ARTICLE AD BOX

I am using a custom TemplatedControl in Avalonia. When binding its property to my ViewModel, data validation runs, but I don't see the full validation error visuals. On the inner TextBox of the control, I see the error text, however the border is not appearing.

Themes/Generic.axaml:

<?xml version="1.0" encoding="utf-8"?> <Styles xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Dashavalanche.UI.Controls"> <Style Selector="local|PathBox"> <Setter Property="Template"> <ControlTemplate> <DataValidationErrors> <TextBox Text="{Binding Path, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" Watermark="{TemplateBinding Watermark}" x:Name="PART_TextBox"/> </DataValidationErrors> </ControlTemplate> </Setter> </Style> </Styles>

Controls/PathBox.cs:

using System; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Data; namespace Dashavalanche.UI.Controls; public class PathBox : TemplatedControl { public static readonly DirectProperty<PathBox, string?> PathProperty = AvaloniaProperty.RegisterDirect<PathBox, string?>( nameof(Path), o => o.Path, (o, v) => o.Path = v, defaultBindingMode: BindingMode.TwoWay, enableDataValidation: true); private string? _path; public string? Path { get => _path; set => SetAndRaise(PathProperty, ref _path, value); } public static readonly StyledProperty<string?> WatermarkProperty = AvaloniaProperty.Register<PathBox, string?>(nameof(Watermark)); public string? Watermark { get => GetValue(WatermarkProperty); set => SetValue(WatermarkProperty, value); } protected override void UpdateDataValidation(AvaloniaProperty property, BindingValueType state, Exception? error) { base.UpdateDataValidation(property, state, error); if (property == PathProperty) { DataValidationErrors.SetError(this, error); } } }

Usage:

<local:PathBox Watermark="Откуда" Path="{Binding SourcePath, Mode=TwoWay}"/>

In the screenshot, the PathBox is on top and on the bottom is a regular TextBox, bound to a property with the same data validation.

screenshot

In the Logic and Visual trees in the DevTools, the PathBox has the :error pseudoclass but the inner TextBox does not.

Using Avalonia 11.3.12, .NET 8.

Read Entire Article