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 )]" />
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.