System.Windows.Markup.XamlParseException > Cannot set a property on object '#FFFFFFFF' because it is in a read-only state

Mar 26, 2014 at 4:12 PM
I'm starting using de CODE.Framework on a WPF project and wanted firstly to say 'Bravo' to Markus and collègues for this framework...

Now I wanted to report and discuss a bug I encounter.
I'm french (!)... so my default local is "fr-FR"...
I'm having on most samples some XamlParseException or InvalidOperationException on application start or when navigating to certain view (see 1 comple trace below)

1) FIX (Temp):
I quickly found an unsatisfactory bug fix by manual setting the local in the App.xaml.cs:ApplicationStartup:
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");

(if you want to reproduce I suppose you just have to set the local to "fr-FR" for example)

2) Investigating
a) Some exceptions involve changing a Brush opacity (having the Brush props IsFrozen=true)... so it throw an exception (see / >> if (brush2 != null) brush2.Opacity = opacity;)
b) In places I've try to clone the Brush before (Microsoft answer) but it changes the final result (?), I've try either:
brush2.Clone() or brush2.CloneCurrentValue()

Looking forward to hearing from the CODE.Framework communities!
Keep the work!


Exception: (or InvalidOperationException)
System.Windows.Markup.XamlParseException occurred
_message=Cannot set a property on object '#FFFFFFFF' because it is in a read-only state.
Message=Cannot set a property on object '#FFFFFFFF' because it is in a read-only state.
   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
InnerException: System.InvalidOperationException
   _message=Cannot set a property on object '#FFFFFFFF' because it is in a read-only state.
   Message=Cannot set a property on object '#FFFFFFFF' because it is in a read-only state.
        at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
        at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
        at System.Windows.Media.Brush.set_Opacity(Double value)
        at CODE.Framework.Wpf.Controls.GridEx.SetBackground(Panel grid, Brush brush, Double lightFactor, Double opacity) in d:\200_Projects\_PoC\CODE.Framework.Source.4.0.40304.0\CODE.Framework.Wpf\Controls\GridEx.cs:line 269
        at CODE.Framework.Wpf.Controls.GridEx.BackgroundBrushLightFactorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) in d:\200_Projects\_PoC\CODE.Framework.Source.4.0.40304.0\CODE.Framework.Wpf\Controls\GridEx.cs:line 312
        at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
        at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
        at System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot)
        at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory templateRoot)
        at System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject container, Object currentObject)
        at System.Windows.FrameworkTemplate.<>c__DisplayClass6.<LoadOptimizedTemplateContent>b__3(Object sender, XamlObjectEventArgs args)
        at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
        at System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property)
        at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
Mar 27, 2014 at 9:39 AM

Thanks! Glad you like the framework :-)

Do you have a sample project that I could try out to see if I can repro this?

Mar 27, 2014 at 3:22 PM
Hello and thanks for your answer!

The classic '' from one of your articles should do the job (I mean the bug!)

In the App.xaml.cs>ApplicationStartup(...)
//ADD something like:
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR");
 //BUG: i 'fix' the bug if add this:
 //       Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");


Has I said from my brief investigation, some exception seems related to Brush being in frozen State (prop IsFrozen=True) so that when you tried to change the brush Opacity...bham!..exception! :)
Code example is: (from GridPrimarySecondary.cs)
   private void SetInternalSecondaryAreaBackgroundBrush()
        if (SecondaryAreaBackgroundBrush == null)
            _secondaryAreaBackgroundBrush = null;

        _secondaryAreaBackgroundBrush = SecondaryAreaBackgroundBrush.Clone();
        _secondaryAreaBackgroundBrush.Opacity = SecondaryAreaBackgroundBrushOpacity;

        if (SecondaryAreaBackgroundBrushLightFactor < .999d || SecondaryAreaBackgroundBrushLightFactor > 1.001d)
            var converter = new LitBrushConverter();
            _secondaryAreaBackgroundBrush = converter.Convert(_secondaryAreaBackgroundBrush, typeof(Brush), SecondaryAreaBackgroundBrushLightFactor, CultureInfo.InvariantCulture) as Brush;
//////////The code going bham! >> as secondaryAreaBackgroundBrush.IsFrozen = True (!,?)
            if (_secondaryAreaBackgroundBrush != null) _secondaryAreaBackgroundBrush.Opacity = SecondaryAreaBackgroundBrushOpacity;
Also I tried in different places to modify the code as to clone the brush prior to access (ie brush = brush.Clone() or brush = brush.CloneCurrentValue())... so the exception is really fixed... but the cloned brush DOESNT behave exacly like the original one! Hope it helps!

No hurry, no stress... just to let you know.

[I've pondered a lot on this bug and I can conceive no (direct) relation in between a brush and my french local... so that I can only begin to suspect that deep within, antangled in the DOTNET Framework is a slight disdain if not rejection of french people builtin by a Microsoft teamworker near the end of the 20th century! (I mean I'm joking...)]

I'm putting together a nice solution including CODE.Framework (of course) and WPF+Entity Code first aggregating all the good practices found around in samples.
And is actualy getting to implements validation from the EF attributes and business rules... anyhow if anyone have interesting sample?...
Thanks to CODE.Framework this is really getting together... and I'm very happy to have fallen (again) into PRISM this time!
So thanks again!