WPF-clear-inkcanvas-with-StrokeCollection
1.网上的资料
如果是需要用brush进行erase那么用System.Windows.Controls.InkCanvasEditingMode.EraseByStroke
如果是希望Clear的话,用Strokes.Clear即可
Strokes是指和InkCanvas binding的对象
本人理解错误,使用Strokes类去清除,结果自然是失败的。
2.解决办法
应该使用实例化后的inkcanvas之中的StrokeCollection.Clear()方法
接下来是代码,注意我使用了prism的mvvm模式
Views的xaml文件
<UserControl x:Class="TensorFlowApp.Views.Handwriting"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TensorFlowApp.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="Gray" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250"></ColumnDefinition>
<ColumnDefinition>
</ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<Button
Style="{StaticResource MaterialDesignRaisedButton}"
Width="100"
Margin="0,50,0,150"
Command="{Binding OpenClearCanvasCommand}"
CommandParameter="{Binding ElementName=inkCanvas1,Path=Strokes}"
ToolTip="Resource name: MaterialDesignRaisedButton">
清除字迹
</Button>
<Button
Style="{StaticResource MaterialDesignRaisedButton}"
Width="100"
ToolTip="Resource name: MaterialDesignRaisedButton" Click="Button_Click">
识别数字
</Button>
</StackPanel>
<InkCanvas Grid.Column="1" DefaultDrawingAttributes="{Binding inkDrawingAttributes}"
x:Name="inkCanvas1" Height="224" Width="224"></InkCanvas>
</Grid>
</UserControl>
ViewsModels的cs文件
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Ink;
using System.Windows.Media;
namespace TensorFlowApp.ViewModels
{
public class HandwritingViewModel : BindableBase
{
//清除画布
public DelegateCommand<StrokeCollection> OpenClearCanvasCommand { get; set ; }
public DrawingAttributes inkDrawingAttributes { get; set; }
public Stroke stroke { get; set; }
public HandwritingViewModel()
{
this.inkDrawingAttributes = new DrawingAttributes();
this.inkDrawingAttributes.Width = 20;
this.inkDrawingAttributes.Height = 20;
this.OpenClearCanvasCommand = new DelegateCommand<StrokeCollection>(ClearCanvasCommand);
}
public void ClearCanvasCommand(StrokeCollection obj)
{
obj.Clear();
}
}
}
3.效果
最后修改: 2022-10-15T22:26:58
版权声明:署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
comment 评论