Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 245423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:05:15+00:00 2026-05-11T21:05:15+00:00

I’m using the lastest version of Silverlight 2.0 within Visual Studio 2008. I have

  • 0

I’m using the lastest version of Silverlight 2.0 within Visual Studio 2008. I have a simple Silverlight UserControl with the following code:

public partial class SilverlightControl1 : UserControl
{
    public SilverlightControl1()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(SilverlightControl1_Loaded);
        Composite = new Composite();
    }

    void SilverlightControl1_Loaded(object sender, RoutedEventArgs e)
    {
        Composite.Width = this.Width / 2.0;
        Composite.Height = this.Height / 2.0;
        if (!this.LayoutRoot.Children.Contains(Composite)) 
            this.LayoutRoot.Children.Add(Composite);
    }

    public Composite Composite
    {
        get;
        set;
    }
}

public class Composite : ContentControl
{
    private Grid grid;
    private Canvas canvas;

    public Composite()
    {
        if (grid == null) grid = new Grid();
        if (canvas == null) canvas = new Canvas();
        if (!grid.Children.Contains(canvas)) 
            grid.Children.Add(canvas);
        Content = grid;
        this.Loaded += new RoutedEventHandler(Composite_Loaded);
    }

    private Rectangle rectangle;

    void Composite_Loaded(object sender, RoutedEventArgs e)
    {
        if (rectangle == null) rectangle = new Rectangle();
        Canvas.SetTop(rectangle, 0);
        Canvas.SetLeft(rectangle, 0);
        rectangle.Fill = new SolidColorBrush(Color);
        rectangle.Width = Width;
        rectangle.Height = Height;
        if (!canvas.Children.Contains(rectangle)) 
            canvas.Children.Add(rectangle);
    }

    public Color Color
    {
        get;
        set;
    }
}

I then use this UserControl in a Silverlight application, the XAML of the page looking like this:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:test="clr-namespace:SilverlightClassLibrary1;assembly=SilverlightClassLibrary1"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="Green">
    <test:SilverlightControl1 Name="uControl1">
      <test:SilverlightControl1.Composite>
        <test:Composite Color="Yellow"/>
      </test:SilverlightControl1.Composite>
    </test:SilverlightControl1>
    </Grid>
</UserControl>

My question is: what code do I have to add to the above so that by changing “Composite Color” to something other than Yellow and hitting the return button, the UserControl automatically refreshes? As the code is, the only way to refresh the UserControl is by moving the Slider bar within the VS2008 IDE which changes the percentage zoom of the Silverlight Page. A side question, although of lesser importance to the above question, is: with the code as it is above, why can’t I change the “Background” color of the LayoutRoot? If I remove my UserControl it works as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:05:16+00:00Added an answer on May 11, 2026 at 9:05 pm

    The solution was two-fold. Firstly to make changes in the LayoutUpdated event rather than the Loaded event and secondly to subscribe to the PropertyChangedCallback of the PropertyMetadata. Here’s the complete working code:

      public partial class SilverlightControl1 : UserControl
      {
        public SilverlightControl1()
        {
          InitializeComponent();
          this.LayoutUpdated += new EventHandler(SilverlightControl1_LayoutUpdated);
          Composite = new Composite();
        }
    
        void SilverlightControl1_LayoutUpdated(object sender, EventArgs e)
        {
          Composite.Width = this.Width / 2.0;
          Composite.Height = this.Height / 2.0;
          if (!this.LayoutRoot.Children.Contains(Composite)) this.LayoutRoot.Children.Add(Composite);
        }
    
        public Composite Composite
        {
          get;
          set;
        }
      }
    
      public class Composite : ContentControl
      {
        private Grid grid;
        private Canvas canvas;
    
        public Composite()
        {
          if (grid == null) grid = new Grid();
          if (canvas == null) canvas = new Canvas();
          if (!grid.Children.Contains(canvas)) grid.Children.Add(canvas);
          Content = grid;
          this.LayoutUpdated += new EventHandler(Composite_LayoutUpdated);
        }
    
        void Composite_LayoutUpdated(object sender, EventArgs e)
        {
          if (rectangle == null) rectangle = new Rectangle();
          Canvas.SetTop(rectangle, 0);
          Canvas.SetLeft(rectangle, 0);
          rectangle.Fill = new SolidColorBrush(Color);
    
          rectangle.Width = Width;
          rectangle.Height = Height;
          if (!canvas.Children.Contains(rectangle)) canvas.Children.Add(rectangle);
        }
    
        public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(Composite), new PropertyMetadata(Colors.Red, new PropertyChangedCallback(OnColorPropertyChanged)));
    
        private static void OnColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          Composite comp = (Composite)d;
          comp.InvalidateArrange();
        }
    
        private Rectangle rectangle;
    
        public Color Color
        {
          get { return (Color)GetValue(ColorProperty); }
          set { SetValue(ColorProperty, value); }
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.