我无法绑定到DataTemplate中的viewmodel属性。我想在ListBox中显示/隐藏全局复选框。
<DataTemplate x:Key="template">
<CheckBox Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}" Background="Gray" cal:Message.Attach="[Action Check( $dataContext )]" />
答案 0 :(得分:1)
之前我遇到过类似的问题。我创建了ViewModelLocator类,它具有我的视图模型的公共属性。这些属性通过IoC容器检索:
public partial class ViewModelLocator
{
public MainPageViewModel MainPageViewModel
{
get { return this.containerLocator.Container.Resolve<MainPageViewModel>(); }
}
}
然后您需要在App.xaml中创建静态资源:
<Application.Resources>
<viewmodels:ViewModelLocator x:Key="ViewModelLocator"/>
</Application.Resources>
最终你可以在DataTemplate中使用它:
<DataTemplate x:Key="template">
<CheckBox Visibility="{Binding MainPageViewModel.IsVisible, Source={StaticResource ViewModelLocator}}"/>
</DataTemplate>