0

Currently I have a UserControl contained within a window. The UserControl is made up of two text boxes. The UserControl is an element in my MainWindow. Outside the scope of my UserControl is my submit button in my window. I would like to enable and disable the button whenever the boxes text contents are not null or null.

UserControl XAML code:

<UserControl x:Class="myClass.myUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d">
    <StackPanel Background="White">
        <DockPanel>
            <Label x:Name="lbl1" Content="First Box:"/>
            <TextBox x:Name="txtbox1"/>
            <Label x:Name="lbl1" Content="Second Box:"/>
            <TextBox x:Name="txtbox2"/>
        </DockPanel>
    </StackPanel>
</UserControl>

View Model:

using System;

namespace myClass {

    partial class UserControlViewModel: ViewModelBase {

        private bool _validInput;

        public UserControlViewModel() {
            validInput = false;
        }

        public object validInput {
            get { return _validInput; }
            set {
            _validInput = value;
            OnPropertyChanged("validInput");
            }
        }
    }

ViewModelBase:

using System.ComponentModel;

namespace myClass {
    class ViewModelBase : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName) {
            var handler = PropertyChanged;
            if (handler != null) {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

My issue is deciding on how to handle this validation, my button's isEnabled property is currently bounded to the validInput boolean of the view model. However, the contents of the user control are not accessible in my window as I have abstracted it as a separate userControl item (I plan on having different user controls available to be shown in the window).

MainWindow XAML:

<Window x:Class="myClass.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:uc="clr-namespace:myClass"
        Title="MainWindow" Height="356" Width="699" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">

    <Window.DataContext>
        <uc:UserControlViewModel/>
    </Window.DataContext>

    <Grid>
        <UserControl x:Name="usrControl"/>

        <Button x:Name="btn" Content="Create" Click="btn_Click" IsEnabled = "{Binding validInput}"/>

    </Grid>

</Window>

MainWindow C#:

using System;
using System.Windows;
using System.Windows.Controls;

namespace myClass {

    public partial class MainWindow: Window {

        UserControlViewModel view;

        public MainWindow() {
            InitializeComponent();
            view = new UserControlViewModel();
            DataContext = view;
        } 
}

I need to be able to check the contents of the text boxes in the UserControl from the MainWindow as my view is in the MainWindow, however the contents are inaccessible to me and it doesn't make sense to have the view in the UserControl. How should I go about solving this?

TheGreatZab
  • 163
  • 1
  • 3
  • 18
  • Add a boolean dependency property to the UserControl that is updated whenever the Text property of the two TextBoxes changes, e.g. in a TextChanged event handler that is attached to both of them. – Clemens Jul 02 '19 at 13:54
  • How will the dependency property effect the view model of the window if the property is in the `UserControl`? – TheGreatZab Jul 02 '19 at 14:30
  • You would just bind it to your view model, with Mode=OneWayToSource. – Clemens Jul 02 '19 at 14:45
  • You should consider implementing IDataErrorInfo on your class. Look at this post for more info: https://stackoverflow.com/questions/13136814/how-to-catch-dataannotations-validation-in-mvvm – Kevin Cook Jul 02 '19 at 15:21
  • @Clemens could you please provide a small working example? I don't see how this works in my case. – TheGreatZab Jul 02 '19 at 16:48

1 Answers1

-1

I've created a similar project. Mainly to do this, validate through your c# code. Basically

(i don't remember id its .content or .text to get the value)

if(txtbox1.Content == ---or--- (textbox1.Content).Equals(Whatever)){

   ----code---

}

else{
    MessageBox.Show("Error")
}

instead of 'disabling' the button (which I don't think you can do) just make it so if invalid, the user knows or just doesn't do anything.

unrelated: if you are wanting a certain input instead of a blank textbox input, you could use this code to give a base if user leaves empty

private void txtbox1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (txtbox1.Text.Equals("your origional text"))
            {
                Name_Text.Text = "";
            }
        }

private void txtbox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (Name_Text.Text.Equals(""))
            {
                Name_Text.Text = "your origional text";
            }
        }

hope this helps

ShaggyMain
  • 16
  • 6