如何从视图模型(WPF,MVVM)更改文本框的visible属性 [英] How to change the visible property of the text box from the view model ( WPF, MVVM)

查看:266
本文介绍了如何从视图模型(WPF,MVVM)更改文本框的visible属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是WPF和MVVM的初学者。我在项目中的窗口上有一些控件。例如,我的窗口中有一个文本框。我正在使用MVVM模式,在这里我想从视图模型中更改文本框的visible属性。

另一件事是,我想根据某些条件从视图模型中更改文本框的可见性。



嗯,我用谷歌搜索了它,谷歌给了我一些建议,这些建议都是不同的解决方案,我完全混淆了。



猜猜有些人可以帮我解决这个问题。



我知道这对于WPF MVVM专家来说是小菜一碟,但是因为我正在尝试学习这些东西,所以我需要一些代码作为例子。



谢谢

Hi,

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.

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.

Thanks

推荐答案

这可能会有所帮助:

XAML:

This might help:
XAML:
<window x:class="TextBoxVisibility.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TextBoxVisibility"
        Title="MainWindow" Height="350" Width="525">
    <window.resources>
        <local:viewmodel x:key="ViewModelClass" xmlns:local="#unknown" />
        <local:converter x:key="Converter" xmlns:local="#unknown" />
    </window.resources>
    <window.datacontext>
        <binding source="{StaticResource ViewModelClass}" />
    </window.datacontext>
    <grid>
        <grid.columndefinitions>
            <columndefinition width="Auto" />
            <columndefinition width="*" />
        </grid.columndefinitions>
        <grid.rowdefinitions>
            <rowdefinition height="Auto" />
            <rowdefinition height="Auto" />
            <rowdefinition height="Auto" />
        </grid.rowdefinitions>
        <label content="FirstTextBox" />
        <label content="SecondTextBox" grid.row="1" />
        <textbox x:name="txt1" text="{Binding Path=Condition,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" grid.column="1" />
        <textbox x:name="txt2" visibility="{Binding Path=IsVisible, Mode=TwoWay, Converter={StaticResource Converter}}" grid.column="1" grid.row="1" />
    </grid>
</window>



ViewModel:


ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace TextBoxVisibility
{
    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            isVisible = false;
            condition = 0;
        }
        private bool isVisible;
        private int condition;
        public int Condition
        {
            get
            {
                return condition;
            }
            set
            {
                condition = value;
                this.OnPropertyChanged("Condition");
                this.OnPropertyChanged("IsVisible");
            }
        }
        public bool IsVisible 
        {
            get
            {
                GetConditon();
                return isVisible;
                
            }
            set
            {
                isVisible = value; ;
                this.OnPropertyChanged("IsVisible");
            }
        }
        private void GetConditon()
        {
            isVisible=condition > 20;
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}



转换器:


Converter:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;

namespace TextBoxVisibility
{
    public class Converter :IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool isVisible = (bool)value;
            return isVisible ? Visibility.Visible : Visibility.Hidden;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}


这篇关于如何从视图模型(WPF,MVVM)更改文本框的visible属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆