Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3691630
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:10:10+00:00 2026-05-19T04:10:10+00:00

Given: WPF 4.0 desktop-based application. Basic input form with two TextBox fields and submit

  • 0

Given:
WPF 4.0 desktop-based application. Basic input form with two TextBox fields and submit button.

XAML-code:

<Label Content="Username" />
<TextBox x:Name="Form_UserName" />

<Label Content="Password" />
<TextBox x:Name="Form_Password" />

<Button x:Name="Submit"
        Click="Form_Submit_Button_Click"
        Content="Submit" />

Task:
Implement logic where submit button is enabled if and only if two TextBox fields are filled.

The classical way to solve this issue is a use of event handlers such as onLostFocus() or something like that, where we can control condition of this fields every time when user switch focus from the field.

But since my project is WPF-based, I prefer to use a native way to work with forms — data binding mechanism. I read some articles from this site and MSDN too about form validation, but in almost all examples is proposed to use MVVM framework and I would like to implement it without any framework.

Also, I tried to play with IMultiValueConverter but no worked result is received.

How to solve the problem with the data binding as simple as possible (I’m only starting with WPF)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:10:11+00:00Added an answer on May 19, 2026 at 4:10 am

    This can be easily done using the WPF validation mechanisms. First since you want to follow the WPF architecture I would reccomend you to use the WPF Command model.

    Now to implement your functionality, you can add a CommandBinding to the Window/UserControl or to the Button itself:

    <Button Content="Save" Command="Save">
    
    <Button.CommandBindings>
        <CommandBinding Command="Save" 
                        Executed="Save_Executed" CanExecute="Save_CanExecute" />
        </Button.CommandBindings>
    </Button>
    

    Now you can subscribe to the CanExecute event to enable or disable your button based on your validation logic. I recommend these reads before you continue:

    Validation in Windows Presentation Foundation

    Using Custom Validation Rules in WPF

    The simplest way to do your requirement is as given below:

    XAML

    <Window x:Class="GridScroll.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:GridScroll"
        Title="Window1" Height="300" Width="300">
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
    
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="200"/>
        </Grid.ColumnDefinitions>
    
        <TextBlock Text="User Name" Grid.Column="0" Grid.Row="0"/>
        <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Text="Password" Grid.Column="0" Grid.Row="1"/>
        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
    
        <Button Content="Save" Grid.Row="2" Grid.ColumnSpan="2" Width="100" HorizontalAlignment="Right" Command="Save">
            <Button.CommandBindings>
                <CommandBinding Command="Save" 
                        Executed="Save_Executed" CanExecute="Save_CanExecute"/>
            </Button.CommandBindings>
    
        </Button>
    </Grid>
    

    Code behind

    public partial class Window1 : Window,INotifyPropertyChanged
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = this;
        }
    
        private string userName;
        public string Username
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
                OnPropertyChanged("UserName");
            }
        }
    
        private string password;
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                OnPropertyChanged("Password");
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    
        private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //Your code
        }
    
        private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = !(string.IsNullOrEmpty(Username) && string.IsNullOrEmpty(Password));
    
        }
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a desktop .NET WPF application witch uses an embedded database (SQLite). Where
Does WPF support using a triggers that respond to routed events but only given
Given 2 rgb colors and a rectangular area, I'd like to generate a basic
I am developing a desktop application that will need to collect data (from the
I have a WPF application which has a DirectX component within it. This component
When creating a UserControl in WPF, I find it convenient to give it some
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a Python object of any kind, is there an easy way to get
Given a select with multiple option's in jQuery. $select = $(<select></select>); $select.append(<option>Jason</option>) //Key =
Given a DateTime representing a person's birthday, how do I calculate their age in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.