如何绑定到 DataTemplate WP7 中的 viewmodel 属性 [英] How to bind to viewmodel property in DataTemplate WP7

查看:25
本文介绍了如何绑定到 DataTemplate WP7 中的 viewmodel 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法绑定到 DataTemplate 中的 viewmodel 属性.我想在 ListBox 中显示/隐藏全局复选框.

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 )]" />

推荐答案

我以前也遇到过类似的问题.我创建了 ViewModelLocator 类,它具有我的视图模型的公共属性.这些属性通过 IoC 容器检索:

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>(); }
    }
}

然后你需要在你的 App.xaml 中创建静态资源:

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

    <Application.Resources>  

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

    </Application.Resources>

最后你可以在 DataTemplate 中使用它:

And finnaly you can use this in DataTemplate:

<DataTemplate x:Key="template">
   <CheckBox Visibility="{Binding MainPageViewModel.IsVisible, Source={StaticResource ViewModelLocator}}"/>
</DataTemplate>

这篇关于如何绑定到 DataTemplate WP7 中的 viewmodel 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆