如何从 ViewModel 更改 WPF 控件的可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1415939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:59:37  来源:igfitidea点击:

How to change a WPF control's visibility from ViewModel

wpfmvvmprism

提问by Raj

I've an WPF application where tried to implement MVVM pattern and Prism 2. I have a Usercontrol which has subscribed to an event fired from another Usercontrol. I would like to toggle visibility of few child elements in the subscribing control. Events are fired properly, even I am successfully able to bind data to some elements. How do I bind Visibility or any style property for that matter with the ViewModel and change them dynamically.

我有一个 WPF 应用程序,试图实现 MVVM 模式和 Prism 2。我有一个用户控件,它订阅了从另一个用户控件触发的事件。我想在订阅控件中切换几个子元素的可见性。事件被正确触发,即使我能够成功地将数据绑定到某些元素。我如何将 Visibility 或任何样式属性与 ViewModel 绑定并动态更改它们。

回答by Ezequiel Jadib

You can have a boolean property in your ViewModel and bind that property to the Visibility property of your controls. Since you will be asigning a boolean value and the Visibility property is expecting a Visibility enumeration value, you will have to use the BooleanToVisibilityConverterconverter to make the conversion,

您可以在 ViewModel 中有一个 boolean 属性并将该属性绑定到控件的 Visibility 属性。由于您将分配一个布尔值并且 Visibility 属性需要一个 Visibility 枚举值,因此您必须使用BooleanToVisibilityConverter转换器进行转换,

<Style.Resources>
     <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />
</Style.Resources>

<Image Visibility="{Binding Path=ShowImage, 
                    Converter={StaticResource booleanToVisibilityConverter}}"/>

Hope this helps.

希望这可以帮助。

Ezequiel Jadib

埃兹奎尔·贾迪布

回答by Darren

Although adding a Boolean property and using a value converter works, I would recommend adding a property of type Visibility to your ViewModel, e.g.

尽管添加布尔属性并使用值转换器有效,但我建议向您的 ViewModel 添加一个 Visibility 类型的属性,例如

public Visibility ImageVisibility
{
    get { return shouldShowImage ? Visibility.Visible : Visibility.Collapsed }
}

The advantage of this method is you don't need to write a converter for every property you want to express in a visual way (e.g. for a stock level that turns a label red when it drops below 10, you could have a converter you use once or just expose a StockLabelBrush property from your VM)

这种方法的优点是您不需要为要以可视方式表达的每个属性编写转换器(例如,对于在低于 10 时将标签变为红色的库存水平,您可以使用转换器一次或仅从您的 VM 公开 StockLabelBrush 属性)

回答by James

There's a simple solution for people who run into this issue.

对于遇到此问题的人,有一个简单的解决方案。

In your view model, create a "Visibility" property like so:

在您的视图模型中,创建一个“可见性”属性,如下所示:

public Visibility ShowModifyButtons
    {
        get { return (Visibility)GetValue(ShowModifyButtonsProperty); }
        set { SetValue(ShowModifyButtonsProperty, value); }
    }

public static readonly DependencyProperty ShowModifyButtonsProperty =
        DependencyProperty.Register("ShowModifyButtons", typeof(Visibility), typeof(FileMatchViewModel),
        new UIPropertyMetadata(Visibility.Collapsed));

In your XAML, bind to it like so:

在您的 XAML 中,像这样绑定到它:

 <Button Focusable="False" Content="Save" Width="100" Margin="10" Visibility="{Binding ShowModifyButtons}"/>

Now, from your view model, you can set ShowModifyButtonsto Visibility.Collapsedor Visibility.Visibleas needed.

现在,从您的视图模型中,您可以根据需要设置ShowModifyButtonsVisibility.CollapsedVisibility.Visible