如何从视图模型(WPF、MVVM)更改文本框的可见属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15545703/
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
How to change the visible property of the text box from the view model ( WPF, MVVM)
提问by Rahul Krishnan R
I am a beginner here in WPF and MVVM. I have certain controls on a window in my project. For example, I have a text box in my window. I am using MVVM Pattern and here I want to change the visible property of the text box from the view model. One other thing is that, I want to change the visibility of the text box from the viewmodel based on some conditions.
我是 WPF 和 MVVM 的初学者。我在我的项目中的一个窗口上有某些控件。例如,我的窗口中有一个文本框。我正在使用 MVVM 模式,在这里我想从视图模型中更改文本框的可见属性。另一件事是,我想根据某些条件更改视图模型中文本框的可见性。
Well, I googled it and google throws me some suggestions which were all different solutions and I'm in a total confusion.
好吧,我用谷歌搜索了它,谷歌给了我一些建议,这些建议都是不同的解决方案,我完全困惑。
Guess some one can help me figure this out.
猜猜有人能帮我解决这个问题。
I know this would be a piece of cake for the WPF MVVM Experts, but since I am trying to learn this stuff I require some code as examples.
我知道这对于 WPF MVVM 专家来说是小菜一碟,但是由于我正在尝试学习这些东西,因此我需要一些代码作为示例。
Thanks
谢谢
回答by Blachshma
Since this is MVVM, you don't want to change the visibility of the textboxyou actually want to disablesome option.. Then - whether that option is enabled or disabled should reflect on the visibility of your Textbox.
由于这是 MVVM,您不想更改文本框的可见性,您实际上要禁用某些选项.. 然后 - 启用还是禁用该选项应反映文本框的可见性。
So basically you want a Propertyin the ViewModel such as:
所以基本上你想要Property在 ViewModel 中,例如:
public bool CanMyPropertyBeChanged {get; set;}
Which you can change (of course you should probably implement INotifyPropertyChangedif you haven't already)...
And bind the visibility of the Textbox to this property, via a Converter:
您可以更改(当然,如果您还没有实现 INotifyPropertyChanged,您可能应该实现)......
并通过转换器将文本框的可见性绑定到此属性:
<TextBox Visibility="{Binding CanMyPropertyBeChanged, Converter={StaticResource boolToVis}}" />
You can use the built-in BooleanToVisibilityConverterfor this:
您可以为此使用内置的BooleanToVisibilityConverter:
<BooleanToVisibilityConverter x:Key="boolToVis" />
回答by WiiMaxx
you can do it multiple way
你可以通过多种方式做到这一点
first of all you could bind it directly
首先你可以直接绑定它
XAML
XAML
<TextBox Visibility="{Binding myVisibility}"/>
VM Property
虚拟机属性
public Visibility myVisibility
{
get { return Visibility.Hidden; }
}
but you could also use a Converter (the recommended way)
但您也可以使用转换器(推荐的方式)
XAML
XAML
xmlns:local="clr-namespace:yourNamespace">
<Window.Resources>
<local:BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
</Window.Resources>
<TextBox Visibility="{Binding myVisibility,Converter={StaticResource BooleanToVisibility}}"/>
VM Property
虚拟机属性
public bool myVisibility
{
get { return false; }
}
BooleanToVisibilityConverter.cs
BooleanToVisibilityConverter.cs
[ValueConversion(typeof(bool),typeof(Visibility))]
public sealed class BooleanToVisibilityConverter : IValueConverter
{
public bool IsReversed { get; set; }
public bool UseHidden { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var val = System.Convert.ToBoolean(value, CultureInfo.InvariantCulture);
if (this.IsReversed)
{
val = !val;
}
if (val)
{
return Visibility.Visible;
}
return this.UseHidden ? Visibility.Hidden : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
回答by yoozz
In you XAML file add the following:
在您的 XAML 文件中添加以下内容:
<Window.Resources>
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />
</ResourceDictionary>
<Window.Resources>
On your textbox add:
在您的文本框中添加:
<TextBox .... Visibility="{Binding IsVisibleBoolean, Converter={StaticResourcebooleanToVisibilityConverter}}" />
In your viewmodel add the IsVisibleBoolean property:
在您的视图模型中添加 IsVisibleBoolean 属性:
public bool IsVisibleBoolean
{
get; set;
}

