Skip to content

Commit 29cf68d

Browse files
committed
chore: add avalonia ListBox demo.
1 parent 98b67b3 commit 29cf68d

File tree

7 files changed

+157
-58
lines changed

7 files changed

+157
-58
lines changed

src/Avalonia/HandyControlDemo_Avalonia/Service/Data/DataService.cs

+30
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@ namespace HandyControlDemo.Service;
1212

1313
public class DataService
1414
{
15+
internal List<DemoDataModel> GetDemoDataList()
16+
{
17+
var list = new List<DemoDataModel>();
18+
for (int i = 1; i <= 20; i++)
19+
{
20+
var dataList = new List<DemoDataModel>();
21+
for (int j = 0; j < 3; j++)
22+
{
23+
dataList.Add(new DemoDataModel
24+
{
25+
Index = j, IsSelected = j % 2 == 0, Name = $"SubName{j}", Type = (DemoType)j
26+
});
27+
}
28+
29+
var model = new DemoDataModel
30+
{
31+
Index = i,
32+
IsSelected = i % 2 == 0,
33+
Name = $"Name{i}",
34+
Type = (DemoType)(i % 6 + 1),
35+
DataList = dataList,
36+
ImgPath = $"/HandyControlDemo;component/Resources/Img/Avatar/avatar{i % 6 + 1}.png",
37+
Remark = new string(i.ToString()[0], 10)
38+
};
39+
list.Add(model);
40+
}
41+
42+
return list;
43+
}
44+
1545
internal List<DemoInfoModel> GetDemoInfo()
1646
{
1747
var infoList = new List<DemoInfoModel>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<UserControl xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
x:Class="HandyControlDemo.UserControl.ListBoxDemo">
4+
<WrapPanel Margin="16">
5+
<ListBox Margin="16"
6+
Width="200"
7+
ItemsSource="{Binding DataList}">
8+
<ListBox.ItemTemplate>
9+
<DataTemplate>
10+
<TextBlock Text="{Binding Name}" />
11+
</DataTemplate>
12+
</ListBox.ItemTemplate>
13+
</ListBox>
14+
<ListBox Margin="16"
15+
Width="200"
16+
ItemsSource="{Binding DataList}"
17+
Theme="{StaticResource ListBox.Small}">
18+
<ListBox.ItemTemplate>
19+
<DataTemplate>
20+
<TextBlock Text="{Binding Name}" />
21+
</DataTemplate>
22+
</ListBox.ItemTemplate>
23+
</ListBox>
24+
</WrapPanel>
25+
</UserControl>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace HandyControlDemo.UserControl;
2+
3+
public partial class ListBoxDemo : Avalonia.Controls.UserControl
4+
{
5+
public ListBoxDemo()
6+
{
7+
InitializeComponent();
8+
}
9+
}

src/Avalonia/HandyControlDemo_Avalonia/ViewModel/Main/MainViewModel.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,20 @@ private void UpdateLeftContent()
7676

7777
//load items
7878
DemoInfoCollection = [];
79-
foreach (var item in _dataService.GetDemoInfo())
79+
Dispatcher.UIThread.InvokeAsync(() =>
8080
{
81-
Dispatcher.UIThread.InvokeAsync(() => DemoInfoCollection.Add(item));
82-
}
83-
Dispatcher.UIThread.InvokeAsync(() => SwitchDemo(DemoInfoCollection.First().DemoItemList.First()));
81+
DataList = _dataService.GetDemoDataList();
82+
83+
foreach (var item in _dataService.GetDemoInfo())
84+
{
85+
DemoInfoCollection.Add(item);
86+
}
87+
88+
if (DemoInfoCollection.Any() && DemoInfoCollection.First().DemoItemList.Any())
89+
{
90+
SwitchDemo(DemoInfoCollection.First().DemoItemList.First());
91+
}
92+
});
8493
}
8594

8695
private void SwitchDemo(DemoItemModel item)

src/Avalonia/HandyControl_Avalonia/Themes/Styles/ListBox.axaml

+80-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
11
<ResourceDictionary xmlns="https://github.com/avaloniaui"
2-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3-
<ControlTheme x:Key="{x:Type ListBox}"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:hc="clr-namespace:HandyControl.Controls">
4+
<ControlTheme x:Key="ListBoxItemBaseStyle"
5+
TargetType="ListBoxItem">
6+
<Setter Property="Padding"
7+
Value="10,0" />
8+
<Setter Property="MinHeight"
9+
Value="{StaticResource DefaultControlHeight}" />
10+
<Setter Property="VerticalContentAlignment"
11+
Value="Center" />
12+
<Setter Property="Background"
13+
Value="{DynamicResource RegionBrush}" />
14+
<Setter Property="BorderBrush"
15+
Value="Transparent" />
16+
<Setter Property="CornerRadius"
17+
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
18+
<Setter Property="BorderThickness"
19+
Value="0" />
20+
<Setter Property="Margin"
21+
Value="0,0,0,2" />
22+
<Setter Property="Template">
23+
<ControlTemplate>
24+
<ContentPresenter Name="PART_ContentPresenter"
25+
Padding="{TemplateBinding Padding}"
26+
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
27+
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
28+
Background="{TemplateBinding Background}"
29+
BorderBrush="{TemplateBinding BorderBrush}"
30+
BorderThickness="{TemplateBinding BorderThickness}"
31+
Content="{TemplateBinding Content}"
32+
ContentTemplate="{TemplateBinding ContentTemplate}"
33+
CornerRadius="{TemplateBinding CornerRadius}" />
34+
</ControlTemplate>
35+
</Setter>
36+
37+
<Style Selector="^:pointerover /template/ ContentPresenter">
38+
<Setter Property="Background"
39+
Value="{DynamicResource SecondaryRegionBrush}" />
40+
</Style>
41+
42+
<Style Selector="^:selected /template/ ContentPresenter">
43+
<Setter Property="Background"
44+
Value="{DynamicResource PrimaryBrush}" />
45+
<Setter Property="Foreground"
46+
Value="{DynamicResource TextIconBrush}" />
47+
</Style>
48+
49+
<Style Selector="^:disabled">
50+
<Setter Property="Opacity"
51+
Value="0.4" />
52+
</Style>
53+
</ControlTheme>
54+
55+
<ControlTheme x:Key="ListBoxBaseStyle"
456
TargetType="ListBox">
557
<Setter Property="Background"
658
Value="{DynamicResource RegionBrush}" />
@@ -11,23 +63,27 @@
1163
<Setter Property="CornerRadius"
1264
Value="{StaticResource DefaultCornerRadius}" />
1365
<Setter Property="Padding"
14-
Value="4" />
66+
Value="2,2,2,0" />
1567
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
1668
Value="Auto" />
1769
<Setter Property="ScrollViewer.VerticalScrollBarVisibility"
1870
Value="Auto" />
1971
<Setter Property="ScrollViewer.IsScrollChainingEnabled"
2072
Value="True" />
73+
<Setter Property="ItemContainerTheme"
74+
Value="{StaticResource ListBoxItemBaseStyle}" />
75+
<Setter Property="hc:BorderElement.CornerRadius"
76+
Value="{StaticResource DefaultCornerRadius}" />
2177
<Setter Property="Template">
2278
<ControlTemplate>
2379
<Border Name="border"
2480
BorderBrush="{TemplateBinding BorderBrush}"
2581
BorderThickness="{TemplateBinding BorderThickness}"
82+
Background="{TemplateBinding Background}"
2683
CornerRadius="{TemplateBinding CornerRadius}">
2784
<ScrollViewer Name="PART_ScrollViewer"
2885
AllowAutoHide="{TemplateBinding (ScrollViewer.AllowAutoHide)}"
2986
BringIntoViewOnFocusChange="{TemplateBinding (ScrollViewer.BringIntoViewOnFocusChange)}"
30-
Background="{TemplateBinding Background}"
3187
HorizontalScrollBarVisibility="{TemplateBinding (ScrollViewer.HorizontalScrollBarVisibility)}"
3288
IsScrollChainingEnabled="{TemplateBinding (ScrollViewer.IsScrollChainingEnabled)}"
3389
IsDeferredScrollingEnabled="{TemplateBinding (ScrollViewer.IsDeferredScrollingEnabled)}"
@@ -42,4 +98,24 @@
4298
</ControlTemplate>
4399
</Setter>
44100
</ControlTheme>
101+
102+
<ControlTheme x:Key="{x:Type ListBox}"
103+
BasedOn="{StaticResource ListBoxBaseStyle}"
104+
TargetType="ListBox" />
105+
106+
<ControlTheme x:Key="ListBoxItemBaseStyle.Small"
107+
BasedOn="{StaticResource ListBoxItemBaseStyle}"
108+
TargetType="ListBoxItem">
109+
<Setter Property="Padding"
110+
Value="6,0" />
111+
<Setter Property="MinHeight"
112+
Value="24" />
113+
</ControlTheme>
114+
115+
<ControlTheme x:Key="ListBox.Small"
116+
BasedOn="{StaticResource ListBoxBaseStyle}"
117+
TargetType="ListBox">
118+
<Setter Property="ItemContainerTheme"
119+
Value="{StaticResource ListBoxItemBaseStyle.Small}" />
120+
</ControlTheme>
45121
</ResourceDictionary>

src/Avalonia/HandyControl_Avalonia/Themes/Styles/ListBoxItem.axaml

-49
This file was deleted.

src/Avalonia/HandyControl_Avalonia/Themes/Theme.axaml

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<MergeResourceInclude Source="/Themes/Styles/ItemsControl.axaml" />
2929
<MergeResourceInclude Source="/Themes/Styles/TabItem.axaml" />
3030
<MergeResourceInclude Source="/Themes/Styles/TabControl.axaml" />
31-
<MergeResourceInclude Source="/Themes/Styles/ListBoxItem.axaml" />
3231
<MergeResourceInclude Source="/Themes/Styles/ScrollViewer.axaml" />
3332
<MergeResourceInclude Source="/Themes/Styles/ListBox.axaml" />
3433
<MergeResourceInclude Source="/Themes/Styles/Label.axaml" />

0 commit comments

Comments
 (0)