This project is read-only.

Using NestedMarkupExtension in a Setter

Jun 23, 2013 at 8:36 PM
Edited Jun 23, 2013 at 9:58 PM
Do you know what needs to be done to allow an update to a property within a setter?

In your code, NestedmarkupExtension.SetPropertyValue, you have the line at the end
pi.SetValue(info.TargetObject, value, null);
This should be wrapped around a try { } catch { } block.

My problem was, on a theme change (response to Microsoft.Win32.SystemEvents.UserPreferenceChanged, I called UpdateNewValue(). This caused an exception "After a “SetterCollectionBase” is in use (sealed), it cannot be modified" with the XAML below. Any idea's how to update the Setter as is with DynamicResourceExtension?

An example XAML is:
<ResourceDictionary ...>
  <Style TargetType="{x:Type ListBoxItem}">
      <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Background" Value="{c:MyExtension ColorKey={x:Static SystemColors.ControlHighlightBrushKey}}"
No workaround until now. Using a <SolidColorBrush x:Key="MyKey" Color="{c:MyExtensionColor ...}"/> didn't work either (where the setter uses {DynamicResource MyKey}), the DependencyProperty raised an exception when setting the setter that the object was read only.

I developed on Win8, tested also on WinXP in a VM, targeting against .NET 4.0. I had thought somehow the MarkupExtension should return "this". But it never enters the code (trace added by me):
            if (!(targetObject is DependencyObject) && !(targetProperty is PropertyInfo)) {
                tsrc.TraceEvent(TraceEventType.Information, 1, "ProvideValue: Think we're a SharedDp, {0}", 
                return this;
Jul 9, 2013 at 9:07 PM
As you've already pointed out - a setter that was once sealed cannot be changed later.
Jul 10, 2013 at 8:57 AM
I've found a solution to this question. Please let me know how I should provide you the code (based on where it works if an extension wishes to update a value assigned to a setter direct (assuming of course you wish to integrate it). I don't have a solution however if a setter uses a dynamic resource where that resource uses the markup extension..
Jul 10, 2013 at 10:35 PM
The best would be if you create a fork on the code base on GitHub and leave a pull request to me.
Thank you for your participation!