Back and forth string format not converting and issues with CanExecute.

don bradman 621 Reputation points
2022-09-20T01:59:39.867+00:00

I've made a WPF app which takes data from a SQLite table and displays them in a datagrid after filtering using the combo box selected item and a further filter textbox if required. I've also added a CRUD form which appears after a row from the datagrid is selected and the respected column values are displayed in some textbox's and datepicker's.

I'm currently having a couple of issues with this app.

Issue 1:
I want the datepicker's to show the date in the format dd-MM-yyyy but when I save the data it should be saved in the database table in the yyyy-MM-dd format, but that is not happening.

Issue 2:
If I add the parameter CanDoSave in the Save button command i.e. SaveCommand = new RelayCommand(DoSave, CanDoSave); where

        bool CanDoSave(object param)  
        {  
  
            var bill = SelectedInv as Bills;  
            float f = 0;  
  
            if (string.IsNullOrEmpty(bill.Party) || string.IsNullOrEmpty(bill.BillNo)|| string.IsNullOrEmpty(bill.BillDt) || string.IsNullOrEmpty(bill.DueDt)||!float.TryParse(bill.Amt, out f))  
            {  
                return false;  
            }  
  
  
            else  
                return true;  
        }  

I get an error
242676-image.png

How do I solve these.

I've shared by project here for checking/testing

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2022-09-26T09:48:19.713+00:00

    For issue 1:

    I bound PaidOn with TextBox in the Remark column for testing, and the code is as follows. You could see if it suits your needs.

    <DatePicker  
     FontSize="14"   
     SelectedDate="{Binding PaidOn , Converter={StaticResource converter}, BindingGroupName=Group1, UpdateSourceTrigger=PropertyChanged}"  
     Grid.Column="1"  
     Grid.Row="6" />  
                        <TextBox  
     FontSize="14"  
     Text="{Binding PaidOn, BindingGroupName=Group1, UpdateSourceTrigger=PropertyChanged}"  
     Grid.Column="1"  
     Grid.Row="7"  
     Foreground="#80D8FF"  
     Background="Transparent" />  
    

    245104-datetimeconverters.txt
    The result:
    After selecting date through Datapicker: The data of the Remark row comes from the data of PaidOn in the data table(replace the original line 7 for testing).
    244811-image.png

    For issue 2:

    You could use Binding Validation to verify whether the input of Item's information is correct. Then trigger the Button Enable based on the validation information.
    For more information, you can refer to How to: Implement Binding Validation.
    Code tested with Amt and BillNo .

    Xaml code:

    245423-enable-button-by-validation.txt

    SaveCommand = new RelayCommand(DoSave);  
    
    
    public class TextBoxNotEmptyValidationRule : ValidationRule  
    {  
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)  
        {  
            string str = value as string;  
            bool f = !string.IsNullOrEmpty(str);  
      
      
            return new ValidationResult(f, Message);  
        }  
        public String Message { get; set; }  
    }  
         
    public class NumericValidationRule : ValidationRule  
    {  
      
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)  
        {  
            decimal result = 1.0M;  
            bool canConvert = decimal.TryParse(value as string, out result);  
            return new ValidationResult(canConvert, Message);  
        }  
        public String Message { get; set; }  
    }  
    

    Button disable:

    245039-image.png
    Button enable:
    244969-image.png

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful