简体   繁体   中英

I can't change brush size in SurfaceInkCanvas

I am having difficulties to understand why I can change the brush color but can't change the brush radius in a SurfaceInkCanvas . Here is what I do:

Double newSize = Math.Round(BrushRadiusSlider.Value,0);

drawingAttributes = new System.Windows.Ink.DrawingAttributes();

// Works :
drawingAttributes.Color = Colors.Yellow;
// Does not work :
drawingAttributes.Width = newSize;
drawingAttributes.Height = newSize;

canvas.DefaultDrawingAttributes = drawingAttributes;

For information, BrushRadiusSlider is a slider in the XAML and gives values between 1 and 100.

See here:
SurfaceInkCanvas.DefaultDrawingAttributes Property

You probably forgot to set the UsesTouchShape to false

The issue is I think that the brush is not updating when the slider's value is changed. Your code above takes the value of the slider at one moment in time, and sets the width and height to that, but it is not linked to the slider.

To get it to update when the slider changes you would need to handle the SliderValueChanged event and reset the drawingAttributes then.

XAML:

<Slider x:Name="BrushRadiusSlider" Minimum="1" Maximum="100" Value="1" ValueChanged="SliderValueChanged"/>

Code:

private void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    if (canvas != null)
    {
        var drawingAttributes = canvas.DefaultDrawingAttributes;
        Double newSize = Math.Round(BrushRadiusSlider.Value, 0);
        drawingAttributes.Width = newSize;
        drawingAttributes.Height = newSize;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM