开发者

How to bind to viewmodel property in DataTemplate WP7

开发者 https://www.devze.com 2023-03-06 20:50 出处:网络
I\'m unable to bind to viewmodel property in DataTemplate. I want to show/hide globally checkboxes in 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: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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号