locked
Fill Color in Selected Stroks Inkcanvas RRS feed

  • Question

  • Hello  there , 

    i have an issue regarding the fill color, means  i have an Inkcanvas and i draw some objects on it , like circle,rectangle,triangle.

    these objects are the collections of Strokscollection on inkcanvas. so my question is that how i fill color on button click  in selected objects.

    Please help me .

    Thanks in advance.


    Om Prakash

    Tuesday, September 27, 2016 2:05 PM

Answers

  • Hi Exotic_Om,

    >>these objects are the collections of Strokscollection on inkcanvas. so my question is that how i fill color on button click  in selected objects.

    As far as I know, You cannot fill color in selected objects (Strokes).

    You may can try the following code. It may be can help you solve your problem.

    XAML:

     <DockPanel>
            <ToolBarTray DockPanel.Dock="Left" Orientation="Vertical" IsLocked="True">
                <ToolBar Padding="2">
                    <RadioButton x:Name="rbDraw" IsChecked="False"
                        ToolTip="Add Rectangle" Margin="3" Checked="rbDraw_Checked">
                        <Rectangle Width="20" Height="12" Stroke="Blue"
                        Fill="LightBlue" />
                    </RadioButton>
                </ToolBar>
            </ToolBarTray>
            <Border BorderThickness="1" BorderBrush="Black">
                <InkCanvas x:Name="canvas1" MouseMove="canvas1_MouseMove" PreviewMouseLeftButtonDown="canvas1_PreviewMouseLeftButtonDown" EditingMode="None">
                </InkCanvas>
            </Border>
        </DockPanel>

    XAML.CS:

    private Polyline polyline;
            private PointCollection polylinePoints;
            private bool drawOnMove = false;
            private List<Polygon> polygons = new List<Polygon>();
    
            public Inkcanvas()
            {
                InitializeComponent();
            }
    
    
            private void canvas1_MouseMove(object sender, MouseEventArgs e)
            {
                if (drawOnMove && (bool)rbDraw.IsChecked)
                {
                    polyline.Points = polylinePoints.Clone();
                    polyline.Points.Add(e.GetPosition(canvas1));
                }
            }
    
            private void canvas1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                if (rbDraw.IsChecked ?? false)
                {
                    if (e.OriginalSource is Ellipse)
                    {
                        canvas1.Children.Remove((Ellipse)e.OriginalSource);
                        canvas1.Children.Remove(polyline);
                        Polygon tmpPolygon = new Polygon();
                        tmpPolygon.StrokeThickness = 2;
                        tmpPolygon.Stroke = Brushes.Black;
                        tmpPolygon.Points = polylinePoints.Clone();
                        polylinePoints.Clear();
    
                        polygons.Add(tmpPolygon);
                        drawOnMove = false;
                        rbDraw.IsChecked = false;
                        tmpPolygon.Fill = Brushes.Gray;
                        canvas1.Children.Add(tmpPolygon);
                        tmpPolygon.MouseDown+=MainWindow_MouseUp;
                    }
                    else
                    {
                        polylinePoints.Add(e.GetPosition(canvas1));
                        polyline.Points = polylinePoints.Clone();
    
                        if (polyline.Points.Count == 1)
                        {
                            Ellipse el = new Ellipse();
                            el.Width = 10;
                            el.Height = 10;
                            el.Stroke = Brushes.Black;
                            el.StrokeThickness = 2;
                            el.Fill = new SolidColorBrush { Color = Colors.Yellow };
                            el.Margin =
                                new Thickness(left: polyline.Points[0].X - el.Width / 2, top: polyline.Points[0].Y - el.Height / 2, right: 0, bottom: 0);
                            canvas1.Children.Add(el);
                        }
    
                        drawOnMove = true;
                    }
                }
            }
    
            private void rbDraw_Checked(object sender, RoutedEventArgs e)
            {
                polyline = new Polyline();
                polylinePoints = new PointCollection();
                polyline.StrokeThickness = 2;
                polyline.Stroke = Brushes.Black;
                canvas1.Children.Add(polyline);
            }
    
            void MainWindow_MouseUp(object sender, MouseButtonEventArgs e)
            {
                Polygon pl = (Polygon)sender;
                pl.Fill = Brushes.SkyBlue;
            }
    

    Best Regards,

    Yohann Lu

    • Proposed as answer by Magnus (MM8)MVP Thursday, September 29, 2016 9:05 PM
    • Marked as answer by DotNet Wang Monday, October 10, 2016 5:27 AM
    Wednesday, September 28, 2016 9:03 AM