feat: add mvvm sample.

This commit is contained in:
rabbitism
2024-04-17 20:10:32 +08:00
parent 13a9806e4b
commit 2c471aa3f6
3 changed files with 99 additions and 19 deletions

View File

@@ -1,27 +1,47 @@
<UserControl 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"
xmlns:u="https://irihi.tech/ursa"
mc:Ignorable="d" d:DesignWidth="800"
d:DesignHeight="450"
x:Class="Ursa.Demo.Pages.TreeComboBoxDemo">
<UserControl
x:Class="Ursa.Demo.Pages.TreeComboBoxDemo"
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"
xmlns:u="https://irihi.tech/ursa"
xmlns:vm="clr-namespace:Ursa.Demo.ViewModels"
d:DesignHeight="450"
d:DesignWidth="800"
x:CompileBindings="True"
x:DataType="vm:TreeComboBoxDemoViewModel"
mc:Ignorable="d">
<StackPanel>
<u:TreeComboBox>
<u:TreeComboBox Width="300" HorizontalAlignment="Left">
<u:TreeComboBoxItem Header="Hello">
<u:TreeComboBoxItem Header="Hello World">
<u:TreeComboBoxItem Header="Hello World 1"/>
<u:TreeComboBoxItem Header="Hello World 2"/>
<u:TreeComboBoxItem Header="Hello World 3"/>
<u:TreeComboBoxItem Header="Hello World 1" />
<u:TreeComboBoxItem Header="Hello World 2" />
<u:TreeComboBoxItem Header="Hello World 3" />
</u:TreeComboBoxItem>
<u:TreeComboBoxItem Header="Hello Avalonia"/>
<u:TreeComboBoxItem Header="Hello Another"/>
<u:TreeComboBoxItem Header="Hello Avalonia" />
<u:TreeComboBoxItem Header="Hello Another" />
</u:TreeComboBoxItem>
<u:TreeComboBoxItem Header="World">
<u:TreeComboBoxItem Header="World Hello"/>
<u:TreeComboBoxItem Header="World Avalonia"/>
<u:TreeComboBoxItem Header="World Another"/>
<u:TreeComboBoxItem Header="World Hello" />
<u:TreeComboBoxItem Header="World Avalonia" />
<u:TreeComboBoxItem Header="World Another" />
</u:TreeComboBoxItem>
</u:TreeComboBox>
<u:TreeComboBox
Width="300"
HorizontalAlignment="Left"
ItemsSource="{Binding Items}">
<u:TreeComboBox.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding ItemName}" />
</TreeDataTemplate>
</u:TreeComboBox.ItemTemplate>
<u:TreeComboBox.SelectedItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ItemName}" />
</DataTemplate>
</u:TreeComboBox.SelectedItemTemplate>
</u:TreeComboBox>
</StackPanel>
</UserControl>

View File

@@ -1,8 +1,67 @@
using System.Collections.Generic;
using CommunityToolkit.Mvvm.ComponentModel;
namespace Ursa.Demo.ViewModels;
public class TreeComboBoxDemoViewModel: ObservableObject
{
public List<TreeComboBoxItemViewModel> Items { get; set; }
public TreeComboBoxDemoViewModel()
{
Items = new List<TreeComboBoxItemViewModel>()
{
new TreeComboBoxItemViewModel()
{
ItemName = "Item 1",
Children = new List<TreeComboBoxItemViewModel>()
{
new TreeComboBoxItemViewModel()
{
ItemName = "Item 1-1",
Children = new List<TreeComboBoxItemViewModel>()
{
new TreeComboBoxItemViewModel()
{
ItemName = "Item 1-1-1"
},
new TreeComboBoxItemViewModel()
{
ItemName = "Item 1-1-2"
}
}
},
new TreeComboBoxItemViewModel()
{
ItemName = "Item 1-2"
}
}
},
new TreeComboBoxItemViewModel()
{
ItemName = "Item 2",
Children = new List<TreeComboBoxItemViewModel>()
{
new TreeComboBoxItemViewModel()
{
ItemName = "Item 2-1"
},
new TreeComboBoxItemViewModel()
{
ItemName = "Item 2-2"
}
}
},
new TreeComboBoxItemViewModel()
{
ItemName = "Item 3"
},
};
}
}
public partial class TreeComboBoxItemViewModel : ObservableObject
{
[ObservableProperty] private string? _itemName;
public List<TreeComboBoxItemViewModel> Children { get; set; } = new ();
}

View File

@@ -6,8 +6,8 @@ using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.Metadata;
using Avalonia.VisualTree;
using Irihi.Avalonia.Shared.Common;
using Size = Avalonia.Size;
@@ -71,6 +71,7 @@ public class TreeComboBox: ItemsControl
public static readonly StyledProperty<IDataTemplate?> SelectedItemTemplateProperty =
AvaloniaProperty.Register<TreeComboBox, IDataTemplate?>(nameof(SelectedItemTemplate));
[InheritDataTypeFromItems(nameof(ItemsSource))]
public IDataTemplate? SelectedItemTemplate
{
get => GetValue(SelectedItemTemplateProperty);