Avalonia笔记2 -数据集合类控件

本文介绍了WPF中的四种常用控件:DataGrid的ItemsSource和ItemTemplate,ItemsRepeater的数据模板和布局,ListBox的多种属性及ComboBox的基本功能。展示了如何设置这些控件的属性以及使用示例。
摘要由CSDN通过智能技术生成

学习笔记:

1.    DataGrid 笔记1中已经记录;

2. ItemsControl 

属性:

ItemsSource:数据源

ItemsControl.ItemTemplate:单项数据模板,内部使用<DataTemplate>

示例:

<ItemsControl Grid.Row="4" ItemsSource="{Binding ItemModels}" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions >
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding LastName}"></TextBlock>
                <TextBlock Text="{Binding FirstName}"></TextBlock>
                
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
    public List<Person> ItemModels { get; set; } = new List<Person>();
    public MainWindowViewModel()
    {
        ItemModels.Add(new Person("eew", "dsjuoj"));
        ItemModels.Add(new Person("Ne22323ids", "dsjuoj"));
        ItemModels.Add(new Person("rrrr", "dsjuoj"));
    }

 水平方向可以根据父元素尺寸自动调整,但垂直方向不可以。

3. ItemsRepeater   具有数据模板和布局模板

需要引用nuget包

<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.0.9" />

属性:

ItemsSource

ItemsRepeater.ItemTemplate

ItemsRepeater.Layout:设置布局的方向,默认垂直

// 该标签内必须设置StackLayout
<ItemsRepeater.Layout>
    <StackLayout Spacing="40" Orientation="Horizontal" />
</ItemsRepeater.Layout>

该标签内必须设置StackLayout。

<ItemsRepeater Grid.Row="5" Grid.RowSpan="3" ItemsSource="{Binding ItemModels}">
    <ItemsRepeater.Layout>
        <StackLayout Orientation="Horizontal"></StackLayout>
    </ItemsRepeater.Layout>
    <ItemsRepeater.ItemTemplate>
        <DataTemplate>
            <Border Margin="0,10,0,0" CornerRadius="5" BorderBrush="Blue" BorderThickness="1" Padding="5">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding FirstName}"></TextBlock>
                    <TextBlock Margin="5 0" Text="{Binding LastName}"></TextBlock>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ItemsRepeater.ItemTemplate>
</ItemsRepeater>

                     

 4.  ListBox

属性:

Items:数据项集合。只有get属性

SelectedIndex:下标

SelectedItem:单选

SelectedItems: 多选

Selection:一个ISelectionModel对象,具有各种方法来跟踪多个选定项目

SelectionMode:选择模式,值为:Single(单选模式)、Multiple(多选模式)、Toggle(所选项目可以切换,若不启用,则需要用“shift/ctrl”切换)、AlwaysSelected(始终被选择)

ScrollViewer.Horizontal:水平滚动条的可见性,值:Disabled(默认)、Auto、Hidden、Visible。

ScrollViewer.Vertical:垂直滚动条的可见性,值:Disabled(默认)、Auto、Hidden、Visible。

示例:

<ListBox Grid.Row="5" x:Name="animals">
 animals.ItemsSource = new string[] { "cat", "dog", "bird", "tiger", "duck", "chicken" }.OrderBy(x=>x);

 

 5. ComboBox  下拉框

属性:

Items:数据项集合,只读

SelectedIndex

SelectedItem

SelectedItems

AutoScrollToSelectedItem:是否自动滚动到所选择的项

IsDropDownOpen:是否打开下拉

MaxDropDownHeight:下拉最大高度,这是列表部分的实际高度,而不是显示的项目数。

示例:

<ComboBox Grid.Row="5" SelectedIndex="0" MaxDropDownHeight="100" x:Name="ComboBox1" ItemsSource="{Binding ComboxArr}"/>
<Button Grid.Row="6" Content="添加列表" Command="{Binding AddCom}" CommandParameter="123"></Button>
public List<string> ComboBoxList = new List<string>();
public ObservableCollection<string> ComboxArr { get; set; }

public MainWindowViewModel()
{
    ComboBoxList.Add("item1");
    ComboBoxList.Add("item2");
    ComboBoxList.Add("item3");
    ComboBoxList.Add("item4");
    ComboBoxList.Add("item5");
    ComboxArr = new ObservableCollection<string>(ComboBoxList.ToArray());
}

public void AddCom(string args)
{
    ComboBoxList.Add(args);
    ComboxArr = new ObservableCollection<string>(ComboBoxList.ToArray());
    this.RaisePropertyChanged(nameof(ComboxArr));
}

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Avalonia是一种用于构建跨平台用户界面的开源框架。导航控件Avalonia中的一种重要控件,用于在不同页面之间进行导航操作。 Avalonia的导航控件允许我们在应用程序中创建多个页面,并且能够在这些页面之间进行平滑的切换。导航控件提供了一种结构化的方式来组织和管理应用程序的不同界面。通过导航控件,我们可以通过简单的命令或代码逻辑来实现页面的导航和跳转。 导航控件通常由两个主要组件组成:导航器和页面容器。导航器负责维护当前页面的状态,并提供导航操作的方法和事件。页面容器用于显示和管理不同的页面。当我们进行页面导航时,导航器会负责加载、显示和销毁页面。 对于使用Avalonia导航控件的应用程序,我们可以在页面间使用导航器提供的方法来切换页面,这样可以实现应用程序的整体流程控制。例如,我们可以使用导航控件在登录页面和主页面之间进行导航,或者在主页面的不同子页面之间进行导航。 此外,Avalonia导航控件还可以与其他控件一起使用,以实现更复杂的用户界面。例如,我们可以将导航控件和菜单控件结合使用,以创建具有导航功能的应用程序菜单。我们也可以将导航控件数据绑定一起使用,以实现基于数据驱动的页面导航。 总而言之,Avalonia导航控件是一种实用的工具,可以帮助我们在Avalonia应用程序中有效地管理和导航页面,提供良好的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值