简体   繁体   English

如何使用MVVM Light在WP7中设置StackPanel的可见性?

[英]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: 这是我的XAML:

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

Note: I have successfully connected my view to my viewmodel. 注意:我已成功将视图连接到视图模型。 All my other properties are binding correctly. 我所有其他属性均正确绑定。

Heres my ViewModel Code: 这是我的ViewModel代码:

    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 在我的机器上工作: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. 将datacontext设置为MainViewModel并在堆栈面板上绑定IsVisible。 I've also bound the button to a command that will change the IsVisible property to Collapsed: (XAML) 我还将按钮绑定到了一个命令,该命令会将IsVisible属性更改为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: 以及视图模型上的IsVisible属性和MakeVisibleCommand属性:

    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. 当然,请确保您的视图模型类继承自ViewModelBase和所有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! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM