none
Cannot set a property on object '#FFFFFFFF' because it is in a read-only state. RRS feed

  • Question

  • Hello,
    I have an object of type System.Windows.Shapes.Rectangle, and when I try to set a property rectangle.Stroke.Opacity = opacity, I get this error:
    A first chance exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll
    Additional information: Cannot set a property on object '#FFFFFFFF' because it is in a read-only state.
    When debugging, I found that the address for Stroke is #FFFFFFFF, but I'm not sure how and when this got set.
    I could work around this by just creating a new Stroke object and setting the rectangle.Stroke equal to it, but then the previous Stroke object is lost.  Plus, I'd rather just figure out why I'm getting this read-only exception.
    I do the same exact thing with System.Windows.Shapes.Ellipse, and it works fine.  Please someone help if you have any idea what's wrong.
    • Moved by Andrew.Wu Wednesday, October 26, 2011 8:26 AM (From:Visual C# General)
    Monday, October 24, 2011 3:09 PM

Answers

  • Hi ChitownDrummer,

    Thank you for your post.

    According to your description, I understand you want to know why you get the error:

    A first chance exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll

    Additional information: Cannot set a property on object ‘#FFFFFFFF‘ because it is read-only state.

    I think it might be you set a freeze brush to your Rectangle.Stroke. Stroke is a freezable property, so we should pay attention when we use it. If your Stroke is freezing, we definitely can’t set the opacity property to it. So we can set the Stroke.Opacity as below:

      Ellipse a = new Ellipse();
                //Ellipse1.Stroke = Brushes.Black;
                Brush brush = Brushes.Black;
                if (brush.IsFrozen)
                {
                    brush = brush.Clone();
                }
                brush.Opacity = 0.2;
                Ellipse1.Stroke = brush;

    If you have any additional questions, please feel free to let me know.

    Have a nice day.

     


    Annabella Luo [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, October 28, 2011 9:27 AM
    Moderator

All replies

  • It would probably be better to define your rectangle (indeed your whole UI) in XAML. If you are studying (or using) WPF, I would consider taking the time to learn XAML rather than ignoring it and trying to get everything done with traditional code. See here for an example:

    http://msdn.microsoft.com/en-us/library/system.windows.shapes.shape.stroke.aspx


    James Finch (MCDST) -- Please vote as helpful if you found this post helpful, or mark as answer if it answered your question.
    Monday, October 24, 2011 3:53 PM
  • Hi ChitownDrummer,

    I'm going to help move your post to the WPF forum for better support.

    Thanks for your understanding.

    Best Regards,


    Andrew Wu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 26, 2011 8:26 AM
  • Hi ChitownDrummer,

    Thank you for your post.

    According to your description, I understand you want to know why you get the error:

    A first chance exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll

    Additional information: Cannot set a property on object ‘#FFFFFFFF‘ because it is read-only state.

    I think it might be you set a freeze brush to your Rectangle.Stroke. Stroke is a freezable property, so we should pay attention when we use it. If your Stroke is freezing, we definitely can’t set the opacity property to it. So we can set the Stroke.Opacity as below:

      Ellipse a = new Ellipse();
                //Ellipse1.Stroke = Brushes.Black;
                Brush brush = Brushes.Black;
                if (brush.IsFrozen)
                {
                    brush = brush.Clone();
                }
                brush.Opacity = 0.2;
                Ellipse1.Stroke = brush;

    If you have any additional questions, please feel free to let me know.

    Have a nice day.

     


    Annabella Luo [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, October 28, 2011 9:27 AM
    Moderator
  • We are temporarily marking this as "Answer", if you have any concerns or new findings; please feel free to unmark this.

    Best regards.


    Annabella Luo [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 1, 2011 2:14 AM
    Moderator