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

发布于 2024-11-06 18:03:53 字数 534 浏览 0 评论 0原文

我一直在尝试从我的视图模型中设置堆栈面板的可见性,但运气不佳。这是我的 XAML:

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

注意:我已成功将视图连接到视图模型。我的所有其他属性都正确绑定。

这是我的 ViewModel 代码:

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

这种方法适用于我一直在使用的所有其他属性,没有任何问题。我只是无法获得转移的可见性。有什么建议吗?

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. All 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有影子陪我不离不弃 2024-11-13 18:03:54

在我的机器上工作:D

我知道,这不是一个很好的答案。也许查看我获得的示例代码会有所帮助:

将数据上下文设置为 MainViewModel 并在堆栈面板上绑定 IsVisible。我还将按钮绑定到一个命令,该命令会将 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>

当然没有隐藏代码可以显示。

以及视图模型上的 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);
        }
    }

当然,请确保您的视图模型类继承自 ViewModelBase 和所有这些爵士乐。您已经提到您的其他属性已正确绑定,但在调试时密切关注“输出”窗口中的绑定错误总是有好处的。

希望这对您有所帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文