简体   繁体   中英

Xamarin Converter not working on IsEnabled Property

I am using a converter code below

namespace someAssembly.Converters
{
    public class ValidateTextLengthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value != 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? 1 : 0;
        }
    }
}

Here is my Xaml

Namespace:

xmlns:converter="clr-namespace:someNamepsace.someAssembly.Converters;assembly=someAssembly"

Resources:

  <ContentPage.Resources>
     <ResourceDictionary>
         <converter:ValidateTextLengthConverter x:Key="validateLength" />
    </ResourceDictionary>
  </ContentPage.Resources>

Usage: (edit I added the textbox being refered by converter)

<StackLayout HorizontalOptions="FillAndExpand">
<Label Text="Name:" />
<Entry x:Name="txtName" Text="{Binding Name}" HorizontalOptions="FillAndExpand" />
</StackLayout>

<Button x:Name="btnSave" Text="Save" 
Command="{Binding SaveCommand}"
HorizontalOptions="EndAndExpand" 
BackgroundColor="{x:Static local:ColorResources.ButtonColorTransparent}" 
IsEnabled="{Binding Source={x:Reference txtName},
Path=Text.Length,
Converter={StaticResource validateLength}}"  />

My main purpose is to disable a button when a textbox length is still zero. It seems that Converter or IsEnabled is not firing. Is there something wrong with my code?

Oh well, There's nothing wrong with the code. The error is in my Bindable property where the value needs to be initialized.

From:

private string _Name; //<-- not initialized, Null
public string Name
{
    get { return _Name; }
    set { SetProperty(ref _Name, value); }
}

To:

private string _Name = string.Empty; //<-- needs to be empty or "" string
public string Name
{
    get { return _Name; }
    set { SetProperty(ref _Name, value); }
}

But inspite of a working converter, I opted to use a DelegateCommand so I can check for the length of more than one properties.

public DelegateCommand SaveCommand { get; private set; }

SaveCommand = new DelegateCommand(SaveItem, canExecute)
        .ObservesProperty(() => this.Name)
        .ObservesProperty(() => this.Title)
        .ObservesProperty(() => this.Description);




public bool canExecute()
{
    return !string.IsNullOrEmpty(this.Name) &&    !string.IsNullOrEmpty(this.Title) && !string.IsNullOrEmpty(this.Description);
}


private async void SaveItem()
    {
       //Put your saving logic here.
    }

I think you should bind to Text property which is bindable, instead of its length. Then you should modify the converter to handle string instead of int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM