Skip to main content

Microsoft Silverlight

Answered Question How to GET the color of FILLRSS Feed

(0)

TBink
TBink

Member

Member

254 points

188 Posts

How to GET the color of FILL

I want to GET the color of the FILL of a Rectangle....  the object xaml

<Rectangle X:Name="Test" Fill="#FFF85B5B" />

 via script I want to get that color value and apply it to another object ... I can't find the way...

on mouse down I tried:

var SndFill = Sender.Fill
var TmpRct = '<Rectangle Canvas.Left="100" Canvas.Top="100" Width="50" Height="50" Fill="'+SndFill+'" StrokeThickness="1" Stroke="blue"  />';

 This fails and when I look at the text output for TmpRct I see   Fill="SolidColorBrush"

Sender.Fill.Color gives a non hexidecimal, long number ...  -500901 

 What's the solution...?

tom


Dave Britton
Dave Bri...

Member

Member

681 points

229 Posts

Re: How to GET the color of FILL

does fill.tostring() work?

=> Dave

Senior Engineer/Dev Manager, Vertigo Software
Vertigo

TBink
TBink

Member

Member

254 points

188 Posts

Answered Question

Re: How to GET the color of FILL

Nope...  In VisStudio I see

  Sender.Fill.toString()       "SolidColorBrush"     String

I did do this work around since I simply want to apply the color of another object ... I can use the long value... so,

var SndFill = Sender.Fill.Color

var TmpRct = '<Rectangle Canvas.Left="'+(XPos-10)+'" Canvas.Top="'+(YPos-10)+'" Width="'+SWd+'" Height="'+SHt+'" Fill="red" StrokeThickness="1" Stroke="blue" Opacity="0.35" IsHitTestVisible="false"/>';

this.TempBlock = this.control.content.CreateFromXaml(TmpRct); this.TempBlock.Fill.Color = SndFill;

.... this works but it bothers me that I can't get a hex value for the color ....  seems to me to be a minor bug in the object model

What I'd like to do is (bold italic):

var TmpRct = '<Rectangle Canvas.Left="'+(XPos-10)+'" Canvas.Top="'+(YPos-10)+'" Width="'+SWd+'" Height="'+SHt+'"  Fill=="'+SndFill +'" StrokeThickness="1" Stroke="blue" Opacity="0.35" IsHitTestVisible="false"/>';

But this fails because its a long number

By the way there's the same issue with Stroke

Sender.Stroke.toString()       "SolidColorBrush"     String

tom

 

Jeff Paries
Jeff Paries

Member

Member

606 points

106 Posts

Answered Question

Re: How to GET the color of FILL

There is a thread on converting the color value you get here:
http://silverlight.net/forums/t/4710.aspx 


 

suyog kale
suyog kale

Member

Member

188 points

98 Posts

Answered Question

Re: How to GET the color of FILL

 its simple

var vobj=document.getElementById("YourSilverlightObject");
    var mainCanvas=vobj.content.findName("RectangleID");
    var mainc=Convert.ToCanvas(mainCanvas);
 
    var sc=Convert.ToSolidColorBrush(mainc.get_background());
    bgcolor=sc.get_color();
    bgcolor=getFillColor(bgcolor);

//-------------------------------------------------------------------------------
//-------Color value convertor function
//-------Hexadecimal value from RGB Value
//-------------------------------------------------------------------------------
function getFillColor(color) {
   var fill = color;
   if (fill < 0) {
      fill = (16777216 + parseInt(fill));
   }
   fill = fill.toString(16);
   if (fill.length > 6) {
      fill = fill.substr(fill.length - 6);
   }
   while (fill.length < 6) {
      fill = "0" + fill;
   }
   fill = "#ff" + fill;
   return fill;
}

(If this has answered your question, please click on "Mark as Answer")
Thanks & Rgds,
Suyog Kale
Senior Software Engineer
************************************************************

TBink
TBink

Member

Member

254 points

188 Posts

Re: How to GET the color of FILL

Many thanks... this is just what I was looking for!

kp2712
kp2712

Member

Member

8 points

7 Posts

Re: How to GET the color of FILL

Hi,I want to do something similar..I want to obtain the background of a canvas(which is a LinearGradientBrush) and assign it to a rectagle's fill property.Can you tell me how to do it?

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities