Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a resource in XAML like this:
HTML
<window.resources>
    <!--Opretter Agents objekt-->
    <local:agents x:key="AgentsList" xmlns:x="#unknown" xmlns:local="#unknown" />
</window.resources>


Now I wish to "update" that resource.
I wish to load from a XML file, and then make the resource use "agents".
C#
agents = FindResource("AgentsList") as Agents;
agents = AgentsXmlSerializer.DeSerialize();


so I want the window resource
HTML
<local:agents x:key="AgentsList" xmlns:x="#unknown" xmlns:local="#unknown" />

to use the agents I just deserialized
C#
agents = AgentsXmlSerializer.DeSerialize();

how do I do this?
Posted
Updated 6-Oct-11 8:49am
v2

1 solution

You'll have to remove the existing resource and then add the new object to the resources
C#
Resources.Remove("AgentsList");
Resources.Add("AgentsList", agents);


I don't understand why you would want to do this, but no matter what you're trying to achieve I'm sure you could do it in a much better way.
Also remember if you refer to the resource with StaticResource then what you do will have no effect, using DynamicResource might but not necessarily have the effect you're trying to achieve.
 
Share this answer
 
Comments
ErikSvendsen 6-Oct-11 16:46pm    
Hi Simon Bang.
Thanks for your answer.

I am new to WPF, so I don't really know where to place the object creation yet. Would it simply be better to place it all in code?

There is no way to update a resource?
I know Resources.Update("AgentsList") doesn't exist, but something like it?!

If no, then I will accept your solution
Simon Bang Terkildsen 6-Oct-11 16:49pm    
well you can also write Resources["AgentsList"] = agents, but the effect is exactly the same as the two lines I posted in the solution.
ErikSvendsen 6-Oct-11 16:52pm    
So if I use Resources["AgentsList"] = agents, and bind it with DynamicResource it will work as an update?
Simon Bang Terkildsen 6-Oct-11 16:53pm    
Most likely, but resources isn't supposed to be used like that, so it really depends on what you're trying to do.
ErikSvendsen 6-Oct-11 17:05pm    
Okay. Thank you very much.
Since it goes beyond the scope of the question I do not really expect you to answer.
But why is resources not supposed to be used like that?

I am trying to do nothing more than my code shows. It is an exercise given by my teacher. I know how to do it in code, but I was interested in the XAML/Code part

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900