Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Binding to two contexts in one Button causes unexpected behavior

$
0
0

When I bind my IsEnabled property on my button the behavior works as expected.
The Command property binding also works as expected.
But, if I bind to both at the same time the IsEnabled is not evaluated when the page loads.

It does not seam to like having two different contexts being bound on the same control.
Is this a bug?

The IsEnabled property is bound like so:
IsEnabled="{Binding Path=IsRunning}"
The Command property is bound like so:
Command="{Binding StopTask, Source={x:Static me:App.TimeStudyViewModel}}"
CommandParameter="{Binding}"

The page is bound like so:
BindingContext="{Binding .,Source={x:Static me:App.TimeStudyViewModel}}"
The ScrollView is bound like so:
BindingContext="{Binding SelectedTask}"

SimpleViewModel inherits INotifyPropertyChanged

public class TimeStudyViewModel : SimpleViewModel
{
    public TimeStudyViewModel(TimeStudy timeStudy)
    {
        StartTask = new Command<TimeStudyTaskViewModel>(async vm => await OnStartTask(vm)); 
        StopTask = new Command<TimeStudyTaskViewModel>(async vm => await OnStopTask(vm)); 
}
    public ICommand StartTask { get; private set; }
    public ICommand StopTask { get; private set; }
    public async Task OnStartTask (TimeStudyTaskViewModel vm)
    {
        vm.StartDtm = DateTime.Now;
    }
    public async Task OnStopTask (TimeStudyTaskViewModel vm)
    {
        vm.StopDtm = DateTime.Now;
    }
}
public class TimeStudyTaskViewModel: SimpleViewModel
{
    public DateTime? StartDtm
    {
        get { return _timeStudyTask.StartDtm; }
        set {
            if (_timeStudyTask.StartDtm != value) {
                _timeStudyTask.StartDtm = value;
                RaisePropertyChanged();
                RaisePropertyChanged ("IsRunning");
            }
        }
    }
    public DateTime? StopDtm
    {
        get { return _timeStudyTask.StopDtm; }
        set {
            if (_timeStudyTask.StopDtm != value) {
                _timeStudyTask.StopDtm = value;
                RaisePropertyChanged();
                RaisePropertyChanged ("IsRunning");
            }
        }
    }
    public bool IsRunning 
    {
        get{

            if (StartDtm.HasValue && !StopDtm.HasValue)
                return true;

            return false;
        }
    }

Viewing all articles
Browse latest Browse all 89864

Trending Articles