开发者

How do you set the visibility of a StackPanel in WP7 using MVVM Light?

I've been trying to set the visibility of a stackpanel from my viewmodel but haven't had much luck. Heres my XAML:

<StackPanel Visibility="{Binding Path=IsVisible}"/>

Note: I have successfully connected my view to my viewmodel. Al开发者_运维知识库l my other properties are binding correctly.

Heres my ViewModel Code:

    private Visibility isVisible;
    public Visibility IsVisible
    {
        get
        {
            return isVisible;
        }
        set
        {
            isVisible = value;
            RaisePropertyChanged("IsVisible");
        }
    }

This approach has worked for every other property I've been using with no problem. I just can't get the visibility to transfer. Any suggestions?


Works on my machine :D

I know, not a great answer. Maybe looking at the sample code I've got will help out though:

Setting the datacontext to the MainViewModel and binding IsVisible on the stackpanel. I've also bound the button to a command that will change the IsVisible property to Collapsed: (XAML)

    <UserControl x:Class="MvvmLight1.MainPage"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         Height="300"
         Width="300"
         DataContext="{Binding Main, Source={StaticResource Locator}}">

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins/MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<Grid x:Name="LayoutRoot">
    <StackPanel Height="100" Visibility="{Binding IsVisible}" HorizontalAlignment="Left" Margin="0,29,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="200">
        <TextBlock Height="23" Name="textBlock1" Text="I'm here!" />
    </StackPanel>
    <Button Content="Make the Stackpanel GO!" Height="23" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="158" Command="{Binding Path=MakeVisibleCommand}" />
</Grid>

No codebehind to show of course.

And the IsVisible property and the MakeVisibleCommand property on the view model:

    private Visibility _isVisible = Visibility.Visible;

    public Visibility IsVisible
    {
        get
        {
            return _isVisible;
        }

        set
        {
            _isVisible = value;

            RaisePropertyChanged("IsVisible");
        }
    }

    public RelayCommand MakeVisibleCommand
    {
        get
        {
            return new RelayCommand(() => IsVisible = Visibility.Collapsed);
        }
    }

Of course, make sure your view model class inherits from ViewModelBase and all that jazz. You've already mentioned that your other properties are bound correctly, but its always good to keep an eye on the Output window for binding errors when you're debugging.

Hopefully that helps out!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜