Skip to content

Commit 3ba099f

Browse files
committed
演示修改颜色
1 parent 16ef626 commit 3ba099f

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
namespace AvaloniaInkCanvasDemo.ViewModels;
1+
using System.Collections.ObjectModel;
2+
using Avalonia.Media;
3+
4+
namespace AvaloniaInkCanvasDemo.ViewModels;
25

36
public class MainViewModel : ViewModelBase
47
{
8+
public MainViewModel()
9+
{
10+
SolidColorBrushCollection =
11+
[
12+
Brushes.Red,
13+
Brushes.Black,
14+
Brushes.Green,
15+
Brushes.Blue,
16+
Brushes.Orange,
17+
Brushes.Purple
18+
];
19+
}
20+
21+
public ObservableCollection<IBrush> SolidColorBrushCollection { get; }
522
}

demo/AvaloniaInkCanvasDemo/Views/MainView.axaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,28 @@
1212
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
1313
<vm:MainViewModel />
1414
</Design.DataContext>
15-
<Grid RowDefinitions="*,Auto"
15+
<Grid RowDefinitions="*,Auto,Auto"
1616
Background="Gray">
1717
<Border Background="White">
1818
<inking:InkCanvas x:Name="InkCanvas"/>
1919
</Border>
20-
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10,10,10,10">
20+
21+
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10,10,10,0">
22+
<ListBox SelectedIndex="0" Background="Transparent" ItemsSource="{Binding SolidColorBrushCollection}" SelectionChanged="SelectingItemsControl_OnSelectionChanged">
23+
<ListBox.ItemsPanel>
24+
<ItemsPanelTemplate>
25+
<StackPanel Orientation="Horizontal"></StackPanel>
26+
</ItemsPanelTemplate>
27+
</ListBox.ItemsPanel>
28+
<ListBox.ItemTemplate>
29+
<DataTemplate DataType="IBrush">
30+
<Ellipse Fill="{Binding .}" Width="20" Height="20"></Ellipse>
31+
</DataTemplate>
32+
</ListBox.ItemTemplate>
33+
</ListBox>
34+
</StackPanel>
35+
36+
<StackPanel Grid.Row="2" Orientation="Horizontal" Margin="10,10,10,10">
2137
<Button x:Name="PenModeButton" Click="PenModeButton_OnClick">Pen</Button>
2238
<Button x:Name="EraserModeButton" Margin="10,0,0,0" Click="EraserModeButton_OnClick">Eraser</Button>
2339
<Button x:Name="SwitchRendererButton" Margin="10,0,0,0" Click="SwitchRendererButton_OnClick">Switch Renderer</Button>

demo/AvaloniaInkCanvasDemo/Views/MainView.axaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Avalonia.Controls;
22
using Avalonia.Interactivity;
3+
using Avalonia.Media;
34
using Avalonia.Skia;
45
using AvaloniaInkCanvasDemo.Views.ErasingView;
56
using DotNetCampus.Inking;
@@ -65,4 +66,15 @@ private void SaveStrokeAsSvgButton_OnClick(object? sender, RoutedEventArgs e)
6566
skCanvas.DrawPath(stroke.Path, skPaint);
6667
}
6768
}
69+
70+
private void SelectingItemsControl_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
71+
{
72+
if (e.AddedItems.Count is 1)
73+
{
74+
if (e.AddedItems[0] is ISolidColorBrush brush)
75+
{
76+
InkCanvas.AvaloniaSkiaInkCanvas.Settings.InkColor = brush.Color.ToSKColor();
77+
}
78+
}
79+
}
6880
}

0 commit comments

Comments
 (0)