1

I have a ViewModel which contains a Boolean property which tells you if the user has authenticated or not.

I have a WrapPanel which is bound to a collection of profiles. The DataTemplate for these profiles has an icon - a closed padlock for when the user is not authenticated and an open one for when the user is authenticated. Ideally these would be bound to the Boolean on the ViewModel but the DataContext for the templates is the individual profile objects.

I have tried,

  • Setting the Source selector in the binding as specified here although it appear Windows Phone 7 does not support x:Reference
  • I tried also the Inversion of Control(?) method detailed here (but containerLocator was not found on my object)
  • I tried applying a Style.Trigger but these are not supported in Windows Phone 7
  • I also tried accessing the XAML elements in the code behind and updating programmatically on event triggers, however I could not get a handle on the Image element inside the DataTemplate
Community
  • 1
  • 1
Brendan
  • 18,771
  • 17
  • 83
  • 114

2 Answers2

0

Edit after comment: WP7 does not support style triggers. But if anyone is looking for this answer on following versions I let the reply below:

I would use a Style Trigger as seen here to update the icon Source property on the fly - as part of the style of your DataTemplate so you would get a hold of your Image.

Qortex
  • 7,087
  • 3
  • 42
  • 59
0

One way I found that works based on an answer by Damian Antonowicz but does not implement the full inversion of control method that he uses, is as follows,

Create a partial class which resolves to your view-model instance under your view-model namespace, e.g.

public partial class ViewModelInstanceLocator
{
    public AppViewModel AppViewModel // Or whatever the type of your view-model is ...
    {
        get 
        { 
            return App.VM; // Or wherever your view model instance is ...
        }
    }
}

Define the other half of the class in your XAML page as a resource so that it can be referred to as a static resource, I did this in my App.xaml so that it could be referred to everywhere,

<ResourceDictionary>
    <viewmodel:ViewModelInstanceLocator x:Key="ViewModelInstanceLocator" />
    ...
</ResourceDictionary>

You may need to include the relevant namespace if there is not already a reference to your view-model namespace e.g. at the top,

    xmlns:viewmodel="clr-namespace:MyAppNamespace.ViewModel"

Finally to bind to the view-model as follows,

{Binding AppViewModel.SomeProperty, Source={StaticResource ViewModelInstanceLocator}}

The binding updates as usual just as if the view-model instance had been referred to through the DataContext. However, it does not work with design-time data.

Community
  • 1
  • 1
Brendan
  • 18,771
  • 17
  • 83
  • 114