0

I'm unable to bind to viewmodel property in DataTemplate. I want to show/hide globally checkboxes in ListBox.

<DataTemplate x:Key="template">

<CheckBox Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}" Background="Gray" cal:Message.Attach="[Action Check( $dataContext )]" />
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
awattar
  • 265
  • 1
  • 2
  • 9
  • 3
    You need to show us what you have, and what's not working. This question is currently way too general. – dlev May 19 '11 at 14:19

1 Answers1

1

I had similar problem before. I've created ViewModelLocator class, which has public properties to my view models. These properties are retrieved through IoC container:

public partial class ViewModelLocator
{
    public MainPageViewModel MainPageViewModel
    {
        get { return this.containerLocator.Container.Resolve<MainPageViewModel>(); }
    }
}

Then you need create static resource in your App.xaml:

    <Application.Resources>  

         <viewmodels:ViewModelLocator x:Key="ViewModelLocator"/>

    </Application.Resources>

And finnaly you can use this in DataTemplate:

<DataTemplate x:Key="template">
   <CheckBox Visibility="{Binding MainPageViewModel.IsVisible, Source={StaticResource ViewModelLocator}}"/>
</DataTemplate>
  • We can set name on User Control or Page Control (it holds ViewModel Data Context) and use it similar to example that you provided above. – awattar Jun 03 '11 at 07:07
  • I tried this but my object does not have the `containerLocator` property, is there some missing code which defines this? – Brendan Feb 25 '13 at 23:16