简体   繁体   中英

Why is my binding not working for my property?

EDIT : solved this by using an IValueConverter which i didn't know existed as @tym32167 said in the comments. ( I still don't understand why the way i did it wouldn't work tho.)

I want to be able to change the BackGroundColor to green if the value saved in my ViewModel is <= 85 but the way i did it doesn't work.

What am i doing wrong? Thanks

Debuger

This is what i tried:

TasksGroupPage.xaml

<Span Text="{Binding TaskDBA}" BackgroundColor="{Binding ResultColor}"/>

Created myself a ResultColor property in my BaseViewModel.cs

private Color resultColor;
public Color ResultColor
{
    get => resultColor;
    set
    {
        resultColor = value;
        NotifyPropertyChanged();
    }
}

NewTaskPageViewModel.cs which is where i have my saving command

 async Task SaveNewTask()
        {

            IsBusy = true;
            await Task.Delay(4000);
            IsBusy = false;

            TasksGroup tasksGroup = new TasksGroup();
            Tasks tasks = new Tasks();

            tasksGroup.TasksGroupDescription = TasksGroupDescription;
            tasksGroup.TasksGroupDate = TasksGroupDate;
            tasks.TaskDuration = TaskDuration;
            tasks.TaskDBA = TaskDBA;
            tasks.TaskDescription = TaskDescription;

// this is where i verify the value

            if (tasks.TaskDBA <= 85)
            {
                ResultColor = Color.Green;
            }


            tasksGroup.Taches = new List<Tasks>() { tasks };


            if(ValidateTasksGroup(tasksGroup) && ValidateTasks(tasks))
            {
                await App.Database.SaveTasksGroupAsync(tasksGroup);

                await Application.Current.MainPage.DisplayAlert("Save", "La tâche a été enregistrée", "OK");
                await Application.Current.MainPage.Navigation.PopAsync();
                

                NotifyPropertyChanged();
            }


        }

Try this to Created myself a ResultColor property in my BaseViewModel.cs

public Color resultColor;
public Color ResultColor
{
    get
    {
        return resultColor;
    }

    set
    {
        SetProperty(ref resultColor, value);
    }

}

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