Quantcast
Viewing all articles
Browse latest Browse all 18858

WPF Button Disabling and Text Input Validation

I have a dialog box that a user enters some input and then it adds that value to another listbox.  In doing so, I am trying to use Validation and have the "Add" button disabled until the user enters something into the box.  This window has 2 buttons... "Add" & "Cancel".  I have the Validation working for the input, however, I still cannot manage to disable the "Add" button AND when I hit cancel, it adds an empty object into the listbox. 

Add Button Code:

<Button x:Name="btnAdd"
        Content="Add"
        Click="btnAddUserInputToCategoryListbox_Click"
        Width="100"
        Margin="5"
        Style="{DynamicResource ResourceKey=GlassButtonForAddCategory}"></Button>

<Style x:Key="GlassButtonForAddCategory" TargetType="{x:Type Button}"><Setter Property="FontSize" Value="20" /><Setter Property="Foreground" Value="White" /><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Button}"><Border x:Name="ButtonBorder" CornerRadius="10,10,10,10" BorderThickness="4,4,4,4" Background="Black" BorderBrush="Silver" RenderTransformOrigin="0.5,0.5"><Grid><Grid.RowDefinitions><RowDefinition Height="*"/><RowDefinition Height="1.7*"/></Grid.RowDefinitions><Border Grid.Row="0" CornerRadius=" 10,10,0,0"><Border.Background><LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"><GradientStop Color="Black" Offset="0.1"/><GradientStop Color="Silver" Offset="1"/></LinearGradientBrush></Border.Background></Border><ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" Grid.RowSpan="2" HorizontalAlignment="Center"/></Grid></Border><ControlTemplate.Triggers><Trigger Property="IsPressed" Value="True"><Setter Property="RenderTransform" TargetName="ButtonBorder"><Setter.Value><TransformGroup><ScaleTransform ScaleX="0.9" ScaleY="0.9"/></TransformGroup></Setter.Value></Setter></Trigger><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding ElementName=txtNewCategory,

Path=(Validation.HasError)}" Value="false" /></MultiDataTrigger.Conditions></MultiDataTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>

  private void btnAddUserInputToCategoryListbox_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ViewModelAddCategoriesWindow dataContext = (ViewModelAddCategoriesWindow)this.DataContext;
                dataContext.ValidateUserInput();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
   private string _userInput;

        public string UserInput
        {
            get { return _userInput; }
            set 
            { 
                _userInput = value;
            }
        }

        public void ValidateUserInput()
        {
            string _input = UserInput;
            if (string.IsNullOrEmpty(_input))
            {
                throw new ApplicationException("Enter a Category or click 'Cancel' to close.");
            }
        }

Above Code is to Validate the Input

TextBox

<TextBox x:Name="txtNewCategory"
                             Grid.Row="1"
                             Width="400"
                             Height="25"
                             Margin="10"><TextBox.Text><Binding Path="UserInput" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"><Binding.ValidationRules><local:ValidateUserInput x:Name="ValidateUser" ValidatesOnTargetUpdated="True"/></Binding.ValidationRules></Binding></TextBox.Text><TextBox.Effect><DropShadowEffect BlurRadius="5"
                                              ShadowDepth="3" /></TextBox.Effect></TextBox>

Validation Rule attached to the TextBox

public class ValidateUserInput : ValidationRule
    {
        public override ValidationResult Validate(object value,
       CultureInfo cultureInfo)
        {
            var str = value as string;
            if (String.IsNullOrEmpty(str))
            {
                return ValidationResult.ValidResult;
            }
            return new ValidationResult(true, null);
        }
        public String Message { get; set; }
    }

And this adds the Input to the Listbox

  public void AddCategory()
        {
            ViewModelAddCategoriesWindow vm = new ViewModelAddCategoriesWindow();
            LoaderService.ShowDialog(vm);
            string _userInput = vm.UserInput;
            AddNewCategoryIndex(_userInput);
        }





Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>