Click here to Skip to main content
14,692,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 21-Mar-13 1:39am
v2

1 solution

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:
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();
        }
    }
}
   
Comments
Rahul Krishnan R 21-Mar-13 8:19am
   
Hi,

What exactly I need is :

I have a text box A and text box B. When the focus from the textbox A is lost, (Condition) I need to check whether the textbox A is empty and if empty, I need to hide the textbox B.

we can put a command on the LostFocus event of textbox A using interaction triggers to achieve the event handling in textbox A, but how can I get the above Condition to a Command in the viewmodel.

Thanks
vpasuleti 22-Mar-13 1:36am
   
change condition property to string and check whether it is null or not
isVisible=condition==null ? true :false;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900