Unificar ViewModel en todo UserControl

I'd like to know how to set a single ViewModel throughout a single UserControl. I'm using an Items container inside a user control (bound to ItemsSource) and it doesn't seem to update with the code-behind replacement of a DataContext (to a code-behind instantiated ViewModel)

What i'm trying to do is change the DataBound foreground color of every text Item in the userControl, and the items inside an Itemscontrol dont seem to change. forcing a datacontext change removed the collection items from display.

I think I'm conceptually Misunderstood here. could anyone help?

preguntado el 08 de noviembre de 11 a las 17:11

The issue really is about using Custom Colors in UserControls. It works fine if I assign DataContext to different ViewModel instances in the code-behind until the populated ItemsControl assigned to a differnt Itemsource. Whats the best way of achieving this behavior? -

2 Respuestas

I could fix the problem by using Storyboards to change color but the problem was still the same. The eventual solution was to access the resources inside the templates, which is possible by browsing the Visual Tree as shown in this tutorial


The peculiar thing though, was that I needed to look for my object inside the initial object returned, as it seems to return the System generated one, which you don't see yourself. Looking for the answer inside this one helped. Also, pushing them into a list of items for easy access later could be a general idea, but MS needs to fix the way of doing this pronto.

respondido 09 nov., 11:20

This didn't work as planned - the Storyboard inside the datatemplate seems to animate only the first item in the itemcollection. - Jay Kannan

I would recommend binding the ItemsSource to a property in your ViewModel.

respondido 08 nov., 11:21

It is actually bound to that. maybe that's why its getting over written. - Jay Kannan

Its not working at all. I'm trying to access ForegroundColor from the parent inside a collection bound to an Items control. all the colorbrush bindings are happening at compile time - i'm unable to change the color of text in the controls inside the collection. - Jay Kannan

how an I access the color in the root from inside the Itemtemplate? - Jay Kannan

Looks like SL5 will handle that. AncestorDependancy or something? - Jay Kannan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.