feat: add mvvm sample.
This commit is contained in:
@@ -1,13 +1,18 @@
|
|||||||
<UserControl xmlns="https://github.com/avaloniaui"
|
<UserControl
|
||||||
|
x:Class="Ursa.Demo.Pages.TreeComboBoxDemo"
|
||||||
|
xmlns="https://github.com/avaloniaui"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:u="https://irihi.tech/ursa"
|
xmlns:u="https://irihi.tech/ursa"
|
||||||
mc:Ignorable="d" d:DesignWidth="800"
|
xmlns:vm="clr-namespace:Ursa.Demo.ViewModels"
|
||||||
d:DesignHeight="450"
|
d:DesignHeight="450"
|
||||||
x:Class="Ursa.Demo.Pages.TreeComboBoxDemo">
|
d:DesignWidth="800"
|
||||||
|
x:CompileBindings="True"
|
||||||
|
x:DataType="vm:TreeComboBoxDemoViewModel"
|
||||||
|
mc:Ignorable="d">
|
||||||
<StackPanel>
|
<StackPanel>
|
||||||
<u:TreeComboBox>
|
<u:TreeComboBox Width="300" HorizontalAlignment="Left">
|
||||||
<u:TreeComboBoxItem Header="Hello">
|
<u:TreeComboBoxItem Header="Hello">
|
||||||
<u:TreeComboBoxItem Header="Hello World">
|
<u:TreeComboBoxItem Header="Hello World">
|
||||||
<u:TreeComboBoxItem Header="Hello World 1" />
|
<u:TreeComboBoxItem Header="Hello World 1" />
|
||||||
@@ -23,5 +28,20 @@
|
|||||||
<u:TreeComboBoxItem Header="World Another" />
|
<u:TreeComboBoxItem Header="World Another" />
|
||||||
</u:TreeComboBoxItem>
|
</u:TreeComboBoxItem>
|
||||||
</u:TreeComboBox>
|
</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>
|
</StackPanel>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@@ -1,8 +1,67 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
|
||||||
namespace Ursa.Demo.ViewModels;
|
namespace Ursa.Demo.ViewModels;
|
||||||
|
|
||||||
public class TreeComboBoxDemoViewModel: ObservableObject
|
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 ();
|
||||||
}
|
}
|
||||||
@@ -6,8 +6,8 @@ using Avalonia.Controls.Shapes;
|
|||||||
using Avalonia.Controls.Templates;
|
using Avalonia.Controls.Templates;
|
||||||
using Avalonia.Input;
|
using Avalonia.Input;
|
||||||
using Avalonia.Layout;
|
using Avalonia.Layout;
|
||||||
using Avalonia.LogicalTree;
|
|
||||||
using Avalonia.Media;
|
using Avalonia.Media;
|
||||||
|
using Avalonia.Metadata;
|
||||||
using Avalonia.VisualTree;
|
using Avalonia.VisualTree;
|
||||||
using Irihi.Avalonia.Shared.Common;
|
using Irihi.Avalonia.Shared.Common;
|
||||||
using Size = Avalonia.Size;
|
using Size = Avalonia.Size;
|
||||||
@@ -71,6 +71,7 @@ public class TreeComboBox: ItemsControl
|
|||||||
public static readonly StyledProperty<IDataTemplate?> SelectedItemTemplateProperty =
|
public static readonly StyledProperty<IDataTemplate?> SelectedItemTemplateProperty =
|
||||||
AvaloniaProperty.Register<TreeComboBox, IDataTemplate?>(nameof(SelectedItemTemplate));
|
AvaloniaProperty.Register<TreeComboBox, IDataTemplate?>(nameof(SelectedItemTemplate));
|
||||||
|
|
||||||
|
[InheritDataTypeFromItems(nameof(ItemsSource))]
|
||||||
public IDataTemplate? SelectedItemTemplate
|
public IDataTemplate? SelectedItemTemplate
|
||||||
{
|
{
|
||||||
get => GetValue(SelectedItemTemplateProperty);
|
get => GetValue(SelectedItemTemplateProperty);
|
||||||
|
|||||||
Reference in New Issue
Block a user