Skip to main content

Microsoft Silverlight

Answered Question Dynamically apply fill and stroke color to shapesRSS Feed

(0)

LilVJ
LilVJ

Member

Member

1 points

10 Posts

Dynamically apply fill and stroke color to shapes

I am developing an app using Silverlight 2 beta 2 which allows a shape to be dragged onto a page. I have would like to be able to allow the user to customise the fill color and stroke color of the shape dynamically however I can't work out how to apply a fill color to a shape in code. Can anyone help please?

texmex5
texmex5

Contributor

Contributor

2239 points

382 Posts

Re: Dynamically apply fill and stroke color to shapes

 myRectangle.Fill = new SolidColorBrus(Color.Red);

Check out Brushes documentation article: http://msdn.microsoft.com/en-us/library/cc189003(VS.95).aspx

and also 

Color documentation for different ways to define colors: http://msdn.microsoft.com/en-us/library/system.windows.media.solidcolorbrush(VS.95).aspx

LilVJ
LilVJ

Member

Member

1 points

10 Posts

Re: Dynamically apply fill and stroke color to shapes

Thank you but I've tried that, my problem is that I cannot get the .Fill within the code behind file so I must be missing something somewhere. I've tried it in the page.xaml.cs and the usercontrol.xaml.cs files and I'm getting nowhere.

 

I get this error:

Error 1 'designEditor.shape' does not contain a definition for 'Fill' and no extension method 'Fill' accepting a first argument of type 'designEditor.shape' could be found (are you missing a using directive or an assembly reference?) C:\SilverLight2\MasterProject\Version1\designEditor\designEditor\Page.xaml.cs 775 27 designEditor

texmex5
texmex5

Contributor

Contributor

2239 points

382 Posts

Re: Dynamically apply fill and stroke color to shapes

 can you post some example code, what object are you trying to color?

LilVJ
LilVJ

Member

Member

1 points

10 Posts

Re: Dynamically apply fill and stroke color to shapes

I have a user control called shape and I have set up its properties in XAML and added some code behind to enable transformation to its size. The shape is displayed in page.xaml and has an x:Name rectangle:

<shape:shape x:Name="rectangle"

Canvas.Left="10"

Canvas.Top="25"/>

I have a color palette x: name 'palette' on the page.xaml so the user can select a colour by clicking on the palette. This is also a user control.

I need to be able to apply communication between the rectangle and the palette. I have added a button for fill and a button for stroke onto my page.xaml so the user can pick a colour then click either the fill button or the stroke button and the button event handler should get the color from the palette and fill that color onto the shape. I have tried to simply change the colour in the page.xaml.cs using the following code:

private void fillBtn_Click(object sender, RoutedEventArgs e)

{

 

if (null != _activeShape)

{

rectangle.Fill =
new SolidColorBrush(Color.Red);

}

but I get the error described earlier. NB: active shape is the shape currently on top of the page. I hope this makes sense.

texmex5
texmex5

Contributor

Contributor

2239 points

382 Posts

Answered Question

Re: Re: Dynamically apply fill and stroke color to shapes

The problem is that your shape type is usercontrol and a usercontrol doesn't contain fill attribute.

But I assume that your usercontrol contains a bunch of other objects .. and you can get to their attributes as long as you have set their x:Name attributes.

I made a simple example.

1. UserControl named ShapeTest that has 3 objects in it: a rectangle and 2 ellipses.

2. in Page.xaml I have

<ShapeTest:MyShape x:Name="aShape" Grid.Row="0" Width="400"/> 

and a Button called aButton.

Now in Page.xaml.cs in Click eventhandler of the button I have the following:

private void aButton_Click(object sender, RoutedEventArgs e)

{

Random r = new Random();

aShape.shapeEllipse1.Fill = new SolidColorBrush(Color.FromArgb((byte)255, (byte)(r.NextDouble() * 255), (byte)(r.NextDouble() * 255), (byte)(r.NextDouble() * 255)));

aShape.shapeEllipse2.Fill = aShape.shapeEllipse1.Fill = new SolidColorBrush(Color.FromArgb((byte)255, (byte)(r.NextDouble() * 255), (byte)(r.NextDouble() * 255), (byte)(r.NextDouble() * 255)));

aShape.shapeRectangle.Stroke = aShape.shapeEllipse1.Fill = new SolidColorBrush(Color.FromArgb((byte)255, (byte)(r.NextDouble() * 255), (byte)(r.NextDouble() * 255), (byte)(r.NextDouble() * 255)));

aShape.shapeRectangle.StrokeThickness = 3;

}

Now if I press the button the attributes of the myShape userconrol are changed.

You can check out the example live from here: http://math.ut.ee/~texmex5/Silverlight/ShapeTest/

Full source code can be downloaded from here: http://math.ut.ee/~texmex5/Silverlight/ShapeTest/ShapeTest.zip

In case I totally misunderstood you and you really want to only change the background color of the shape usercontrol then you can do it like this:

aShape.Background = new SolidColorBrush(Colors.Purple);

LilVJ
LilVJ

Member

Member

1 points

10 Posts

Re: Re: Dynamically apply fill and stroke color to shapes

Thank you, I will take some time to look through your example and try to apply it to my app, I'm a bit slow, still learning. I'll let you know if I can make it work.

texmex5
texmex5

Contributor

Contributor

2239 points

382 Posts

Re: Re: Dynamically apply fill and stroke color to shapes

 sure, and surely make another post if something comes up :)

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Re: Dynamically apply fill and stroke color to shapes

Hey I think you are creating a UserControl called shape

inside the usercontrol in the codebehind add the line

public SolidColorBrush Fill
{
    get
    {
       return (SolidColorBrush)LayoutRoot.Background;
    }
    set
    {
        LayoutRoot.Background = (SolidColorBrush)value;
    }
}

After created the property yourself, you can set or get it coz it is a public property defined.

 

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

LilVJ
LilVJ

Member

Member

1 points

10 Posts

Re: Re: Dynamically apply fill and stroke color to shapes

Thank you, the sample you gave me led me to the solution for my problem and I am now able to dynamically fill the boxes. Is there any way to use the hex value of a colour in the reference rather than the color name?

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Re: Dynamically apply fill and stroke color to shapes

OF course, you can use the colorConverter

 Things like here? a colorConverter

http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2008/04/29/colorconverter-in-silverlight-2.aspx

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

LilVJ
LilVJ

Member

Member

1 points

10 Posts

Re: Re: Re: Dynamically apply fill and stroke color to shapes

Thank you, I found a ColorPicker app by Page Brooks which I have managed to integrate into my app, I have included it into my page.xaml and it works great on its own. My problem now is that I don't know how to apply the selected color as my fill color. The selected color is collected and converted from within the ColorPicker User Control (ColorPicker.xaml.cs). The Color Picker app clearly converts the hexvalue and uses the selected color to fill a rectangle on its xaml. My problem is that I don't know how to grab that color and use it in my page.xaml.cs, any ideas?

 

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Re: Re: Dynamically apply fill and stroke color to shapes

As I know the sample inlcude the lines

            Color c = m_colorSpace.GetColorFromPosition(huePos * gradientStops);
            rectSample.Fill = new SolidColorBrush(c);

Then what you can do is 

             myUserControl.Fill = new SolidColorBrush(c);

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

LilVJ
LilVJ

Member

Member

1 points

10 Posts

Re: Re: Re: Re: Dynamically apply fill and stroke color to shapes

I wish it was that simple, and maybe it is but I'm missing something. I have incorporated the Color Picker into my app however I have added a button for fill and a button for stroke. The idea is that the user clicks on the color picker to choose the color they want and then clicks either the Fill button or the Stroke button to apply the chosen color to the active shape. At the moment I am simply adding a color eg:

if (null != _selectedShape)

{

_selectedShape.rect.Fill =
new SolidColorBrush(Colors.Red);

}

if (null != _selectedCircle)

{

_selectedCircle.ellipse.Fill =
new SolidColorBrush(Colors.Red);

}

What I would like to do is add the chosen color from the ColorPicker user control when the fill button is clicked. 

I have tried adding the code you suggest into the fill button event handler but this won't work. I am very much a novice so I could just be missing a declaration somewhere, all help is very welcome.

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Re: Re: Re: Dynamically apply fill and stroke color to shapes

 Ok let me explain how it works

  1. Import the ColorPicker to your own project by changing the namespaces on the corresponding code
  2. Add the ColorPicker to be a UserControl on the LayoutRoot
    Let say
        ColorPicker myCP = new ColorPicker();
        LayoutRoot.Children.Add(myCP);
  3. Change the code in ColorPicker.xaml.cs
    • Add a new var as follow
              public Color c;

    • Change the line in UpdateSelection(int yPos)
      Color c = m_colorSpace.GetColorFromPosition(huePos * gradientStops);
      c = m_colorSpace.GetColorFromPosition(huePos * gradientStops);
  4. Then you can set the color Fill in Page.xaml.cs by this way
    _selectedCircle.ellipse.Fill = new SolidColorBrush(myCP.c);

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

LilVJ
LilVJ

Member

Member

1 points

10 Posts

Answered Question

Re: Re: Re: Re: Re: Dynamically apply fill and stroke color to shapes

Thank you so much, you have led me directly to the solution, I can't believe I have been so thick. Thank you!!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities