【Avalonia】Avalonia的学习笔记以及与WPF的不同点

发布时间 2023-12-13 23:08:22作者: 梦琪小生

1.axaml中引用命名空间

xmlns:model="using:IDataTemplateSample.Models"

2.Grid支持行列的简化写法

<Grid RowDefinitions="Auto, Auto, *" ColumnDefinitions="Auto, *"/>

3.DataTemplate 根据DataType自动选择对应类型的样式,使用Window.DataTemplates加载多个DataTemplate自动选择显示不同样式

<Window.DataTemplates>
        <!--  Add a DataTemplate for a Student  -->
        <!--  Mind the order of the Templates. Begin with the most specific first.  -->
        <DataTemplate DataType="model:Student">
            <StackPanel>
                <TextBlock FontWeight="Bold" Text="{Binding Grade, StringFormat='I am a student in {0}. grade'}" />
                <!--  We re-use the PersonTemplate here by using DynamicResource  -->
                <ContentControl Content="{Binding}" ContentTemplate="{DynamicResource My.DataTemplates.Person}" />
            </StackPanel>
        </DataTemplate>

        <!--  Add a DataTemplate for a Teacher  -->
        <DataTemplate DataType="model:Teacher">
            <StackPanel>
                <TextBlock FontWeight="Bold" Text="{Binding Subject, StringFormat='I am a teacher for: &quot;{0}&quot;'}" />
                <!--  We use a UserControl here to display the data  -->
                <view:PersonView />
            </StackPanel>
        </DataTemplate>

    </Window.DataTemplates>