Original HTTPS://STACKOVERFLOW.COM/QUESTIONS/28240528/HOW-DO-I-DUPLICATE-A-RESOURCE-REFERENCE-IN-CODE-BEHIND-IN-WPF
In my application, I has a color resources. I have one element, that uses, that color asADynamicResourceinchXAML. <window x:class="Resourceplay.mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow"height=" -"Width="425"> <Window.Resources> <color x:key="MyColor">Red</Color> </Window.Resources> <Grid> <rectangle verticalalignment="Top"Width=" the"height=" the"margin="Ten"> <Rectangle.Fill> <solidcolorbrush x:name="Topbrush"Color="{DynamicResource MyColor}"/> </Rectangle.Fill> </Rectangle> <rectangle verticalalignment="Bottom"Width=" the"height=" the"margin="Ten"> <Rectangle.Fill> <solidcolorbrush x:name="Bottombrush"/> </Rectangle.Fill> </Rectangle> </Grid> </Window>in the code, I want to duplicate Thisresource Reference. usingSystem.Windows; usingSystem.Windows.Media; namespaceResourceplay { Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); //I want to copy the resource reference, not the color.Bottombrush.color =Topbrush.color; //I ' d really rather do something like this. varReference =topbrush.getresourcereference (Solidcolorbrush.colorproperty); Bottombrush.setresourcereference (reference); //I want the colors of both elementsresources["MyColor"] =Colors.green; }}}however, setresourcereference only works forFrameworkelements or frameworkcontentelements. SolidColorBrush isJust a Freezable. Also, I had no idea how toGetA resource referenceinchcode behind. Is there a -to Do This inchWPF So, both of the colors change at the same time? In my real application, the problem isn'T quite so simple, so I can't just add a second DynamicResourceinchXaml.
Il Vic suggestedusingReflection. Expanding on that, I is able to build some extension methods forDependencyObject that DoWhat I want. I Don't really like using reflection in code, and if someone else knows a better by-implement this, I'D love-to-see it. At least This'll be helpful whenever I'm trying to debug dynamicresources from code behind. Public Static classdependencyobjectextensions { Public Static ObjectGetdynamicresourcekey ( ThisDependencyObject obj, DependencyProperty prop) { //get the value entry from the Depencency object for the specified dependency property varDependencyObject =typeof(DependencyObject); varDependencyobject_lookupentry = Dependencyobject.getmethod ("LookupEntry", BindingFlags.NonPublic |bindingflags.instance); varEntryindex = Dependencyobject_lookupentry.invoke (obj,New Object[] {prop. Globalindex}); varEffectivevalueentry_getvalueentry = Dependencyobject.getmethod ("Getvalueentry", BindingFlags.NonPublic |bindingflags.instance); varValueentry = Effectivevalueentry_getvalueentry.invoke (obj,New Object[] {entryindex, prop,NULL,0x10 }); //Look inside the value entry to find the Modifiedvalue object varEffectivevalueentry =Valueentry.gettype (); varEffectivevalueentry_value = Effectivevalueentry.getproperty ("Value", BindingFlags.Instance |bindingflags.nonpublic); varEffectivevalueentry_value_getter = Effectivevalueentry_value.getgetmethod (nonpublic:true); varRawentry = Effectivevalueentry_value_getter.invoke (Valueentry,New Object[0]); //Look inside the Modifiedvalue object to find the Resourcereference varModifiedvalue =Rawentry.gettype (); varModifiedvalue_basevalue = Modifiedvalue.getproperty ("BaseValue", BindingFlags.Instance |bindingflags.nonpublic); varModifiedvalue_basevalue_getter = Modifiedvalue_basevalue.getgetmethod (nonpublic:true); varResourcereferencevalue = Modifiedvalue_basevalue_getter.invoke (Rawentry,New Object[0]); //Check the resourcereference for the original ResourceKey varResourcereference =Resourcereferencevalue.gettype (); varResourcereference_resourcekey = Resourcereference.getfield ("_resourcekey", BindingFlags.NonPublic |bindingflags.instance); varResourceKey =Resourcereference_resourcekey.getvalue (Resourcereferencevalue); returnResourceKey; } Public Static voidSetdynamicresourcekey ( ThisDependencyObject obj, DependencyProperty prop,ObjectResourceKey) { varDynamicResource =Newdynamicresourceextension (ResourceKey); varResourcereferenceexpression = Dynamicresource.providevalue (NULL); Obj. SetValue (prop, resourcereferenceexpression); }}the second method uses dynamicresourceextension to avoid some nastiness with Activator, but the first Method feels Inc Redibly Brittle. I can use these methodsinchMy original example asfollows: PublicMainWindow () {InitializeComponent (); varKey =Topbrush.getdynamicresourcekey (Solidcolorbrush.colorproperty); Bottombrush.setdynamicresourcekey (Solidcolorbrush.colorproperty, key); resources["MyColor"] =Colors.green; }this 'll work forAny DependencyProperty, provided it is SetTo a DynamicResource when weTryToGetThe resource key. A little more finesse would be needed forProduction Code.
How does I duplicate a resource reference in code behind in WPF?