ValidationTest.axaml代码

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Height="508" Width="409"
        xmlns:local="using:AvaloniaUI.Demos.Book._19.StoreDatabase"
        x:Class="AvaloniaUI.ValidationTest"
        Title="ValidationTest">
    
    <Grid RowDefinitions="*,auto,2*">
        <!-- 上半部分:产品列表 -->
        <ListBox x:Name="lstProducts"
                         x:DataType="local:Product"
                         DisplayMemberBinding="{Binding ModelName}"
                         Margin="5">
        </ListBox>

        <GridSplitter Grid.Row="1"
               HorizontalAlignment="Stretch"
               VerticalAlignment="Bottom"
               ResizeBehavior="PreviousAndNext"
               Height="5"/>

        <!-- 下半部分:详细信息。-->
        <Border Grid.Row="2" x:Name="border"
                 DataContext="{Binding #lstProducts.SelectedItem}"
                x:DataType="local:Product"
                Padding="7"
                Margin="7"
                Background="LightSteelBlue">
            
            <Grid ColumnDefinitions="auto,*" RowDefinitions="auto,auto,auto,auto,*">
                <!-- Model Number -->
                <TextBlock Margin="7">Model Number:</TextBlock>
                <TextBox Margin="5"
                         Grid.Column="1"
                         Width="150"
                         Text="{Binding ModelNumber}">
                </TextBox>

                <!-- Model Name -->
                <TextBlock Margin="7"
                           Grid.Row="1">
                    Model Name:
                </TextBlock>
                <TextBox Margin="5"
                         Grid.Row="1"
                         Grid.Column="1"
                         Width="150"
                         Text="{Binding ModelName}">
                </TextBox>

                <!-- Unit Cost -->
                <TextBlock Margin="7"
                           Grid.Row="2">
                    Unit Cost:
                </TextBlock>
                <NumericUpDown  Margin="5"
                         Grid.Row="2"
                         Grid.Column="1"
                         Width="150"
                         Value="{Binding UnitCost}">
                </NumericUpDown >

                <!-- Description -->
                <TextBlock Margin="7,7,7,0"
                           Grid.Row="3">
                    Description:
                </TextBlock>
                <TextBox Margin="7"
                         Grid.Row="4"
                         Grid.Column="0"
                         Grid.ColumnSpan="2"
                         TextWrapping="Wrap"
                         AcceptsReturn="True"
                         Height="80"
                         Text="{Binding Description}">
                </TextBox>
            </Grid>
        </Border>
    </Grid>
</Window>

ValidationTest.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using AvaloniaUI.Demos.Book._19.StoreDatabase;
using System.ComponentModel;

namespace AvaloniaUI;

public partial class ValidationTest : Window
{
    private StoreDb1 db = new StoreDb1();
    public ValidationTest()
    {
        InitializeComponent();
        var products = db.GetProducts();
        foreach (var p in products)
        {
            p.ErrorsChanged += OnProductValidationFailed;
        }
        lstProducts.ItemsSource = products;
    }

    private void OnProductValidationFailed(object? sender, DataErrorsChangedEventArgs e)
    {
        if(border == null)
                return;

        if (lstProducts.SelectedItem is INotifyDataErrorInfo product)
        {
            bool hasErrors = product.HasErrors;

            border.BorderBrush = hasErrors ? Brushes.Red : Brushes.Transparent;
            border.BorderThickness = hasErrors ? new Thickness(2) : new Thickness(0);
        }
        else
        {
            border.BorderBrush = Brushes.Transparent;
            border.BorderThickness = new Thickness(0);
        }
    }
}

运行效果

image

 

posted on 2026-01-01 08:54  dalgleish  阅读(0)  评论(0)    收藏  举报