Search code examples
c#.netsilverlightexpression-blend

How to set minimum width and height in a silverlight OOB application?


I am trying to set a minimum width and height for my silverlight 4 OOB application without any success so far. Can someone help me as i keep getting this error messages:

"An object reference is required for the non-static field,method, or property 'kat.MainPage.Width.get' and 'kat.MainPage.Height.get'

My code is the following:

namespace kat
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
      this.SizeChanged +=new System.Windows.SizeChangedEventHandler(LayoutRoot_SizeChanged);
    }

    public double Width { get; set; }
    public double Height { get; set; }

    private void LayoutRoot_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
    {
      if (kat.MainPage.Width <500)
        kat.MainPage.Width =500;
      if (kat.MainPage.Height <500)
        kat.MainPage.Height =500;
    }
  }
}

Solution

  • I assume kat is just your namespace...

    You are basically trying to access members of an object without actually using a pointer to the object. kat.MainPage is a class, not an object so any references to kat.MainPage.anything will fail with that error.

    You just wanted:

    private void LayoutRoot_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
    {
        if (Application.MainWindow.Width < 500)
            Application.MainWindow.Width = 500;
        if (Application.MainWindow.Height < 500)
            Application.MainWindow.Height = 500;
    }