How to bind to viewmodel property in DataTemplate WP7
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:Messa开发者_如何转开发ge.Attach="[Action Check( $dataContext )]" />
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>
精彩评论