1

I have a resource (myPoint) defined in XAML as follows:

<Path Name="myPath"> <Path.Resources> <Point x:Key="myPoint" X="200" Y="200"/> </Path.Resources> </Path> 

In the OnApplyTemplate override in the corresponding C# code, I use the following to get a reference to this resource.

myPointReference = (Point)myPath.FindResource("myPoint"); 

Now, whenever I modify the value of myPointReference, let's say,

myPointReference.X = 30; 

it doesn't modify the value of the resource myPoint, it only changes the value of myPointReference. How can I make myPointReference an actual reference to my XAML defined resource myPoint so that when I modify the value of myPointReference, I am also modifying the value of myPoint?

4
  • 2
    A Point is a value type. Every access to it is a copy, therefore you cannot do that. What is the larger effect you're trying to achieve? There may be another way to do it. Commented Oct 6, 2010 at 23:08
  • 1
    On top of that you will not be able to change the XAML itself by the current means. If that is what you are stating? If you kill the App and fire it back up it's not going to be as though those values will change...unless I am confused on what you are trying to accomplish... Commented Oct 6, 2010 at 23:11
  • I've seen examples of value types in C# being passed by reference, using the ref keyword, but I wasn't able to get that to work in this situation, since I'm not using a function. The bigger picture is this: I have a shape that is being drawn using points as dynamic resources. The hope was that in C# I could change the values of these resources to change the shape. I could use dependency properties, but I don't want to expose the properties to the user of my class. I want them to use my functions to manipulate the shape. Commented Oct 6, 2010 at 23:18
  • I'm trying to use private dependency properties. It isn't working yet. Must be a problem with the bindings, I'm trying to figure it out. If there's another BETTER way, please let me know. Commented Oct 6, 2010 at 23:51

1 Answer 1

1

Under the covers when you use the Resources extension in XAML you are dealing with a ResourceDictionary and you are dealing with adding a struct (aka value type) to this which will be passed by value (aka copied) into this dictionary, and then when you request it you will be returned a copy.

There is no way to pass value types by reference when dealing with a ResourceDictionary.

However, if you really need to do this after you modify your copy you can replace the old copy in the dictionary with your modified copy.

You can try this by calling:

myPath.SetResourceReference("myPoint", myPointReference) 

You may want to consider not doing this in XAML or using a ResourceDictionary. You can just manage the Point in the code behind only and update it and set it to the Path as needed.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.