C# avalonia的绑定,是预编译绑定,所以比Maui会强很多。采用Avalonia支持的绑定语法。

MultipleBindings.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="300" Width="300"
        x:Class="AvaloniaUI.MultipleBindings"
        Title="MultipleBindings">
    <Grid Margin="5" RowDefinitions="auto,auto,auto,auto">
        <Slider Name="sliderFontSize" Margin="3" Minimum="1" Maximum="40" Value="10"></Slider>
        
        <TextBox Name="txtContent" Margin="3" Grid.Row="1">Sample Content</TextBox>
        
        <ListBox Margin="3" Grid.Row="2" Name="lstColors">
            <ListBoxItem Tag="Blue">Blue</ListBoxItem>
            <ListBoxItem Tag="DarkBlue">Dark Blue</ListBoxItem>
            <ListBoxItem Tag="LightBlue">Light Blue</ListBoxItem>
        </ListBox>

        <TextBlock Margin="3" Name="lblSampleText" Grid.Row="3"
                   Text="{Binding #txtContent.Text}"
                   Foreground="{Binding #lstColors.((ListBoxItem)SelectedItem).Tag}"
                   FontSize="{Binding #sliderFontSize.Value}">
            
        </TextBlock>

    </Grid>
</Window>

MultipleBindings.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace AvaloniaUI;

public partial class MultipleBindings : Window
{
    public MultipleBindings()
    {
        InitializeComponent();
    }
}

运行效果

image

 

posted on 2025-08-05 07:58  dalgleish  阅读(31)  评论(0)    收藏  举报