简体   繁体   中英

Binding IsChecked property with a radio button group in xamarin MVVM

I am looking for a way to control the text of a label via radio buttons. I have read through similar questions on this site as well as others without being able to find a working solution. I have tried using a converter as well as putting the radio buttons into a list. When I run the code through my debugger, the code for the radio buttons in the view model are never reached. Any Help would be appreciated. Here is the code for my View:

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="sampleRadioButtons.Views.ButtonPage"
                 xmlns:vm="clr-namespace:sampleRadioButtons.ViewModels">
    
    
            <ContentPage.BindingContext>
                <vm:MainPageViewModel/>
            </ContentPage.BindingContext>
                     
            <ContentPage.Content>
                <StackLayout VerticalOptions="Center">
                    <StackLayout Orientation="Horizontal" RadioButtonGroup.GroupName="time">
                        <RadioButton Content="One Year" IsChecked="{Binding YearChecked, Mode=TwoWay}"/>
                        <RadioButton Content="One Month" IsChecked="{Binding MonthChecked, Mode=TwoWay}"/>
                        <RadioButton Content="One Week" IsChecked="{Binding WeekChecked, Mode=TwoWay}"/>
                    </StackLayout>
                    <Label Text="Total is"/>
                    <Label Text="{Binding Total}"/>
                </StackLayout>
            </ContentPage.Content>
        </ContentPage>

Here is my corresponding view model:

            using System;
            using System.ComponentModel;
            using System.Runtime.CompilerServices;
            
            namespace sampleRadioButtons.ViewModels
            {
                public class MainPageViewModel : INotifyPropertyChanged
                {
                    public MainPageViewModel()
                    {
                        total = GetTotal();
                    }
                    public void OnPropertyChange([CallerMemberName] string name = "")
                    {
                        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
                    }
            
                    #region INotifyPropertyChanged
                    public event PropertyChangedEventHandler PropertyChanged;
                    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
                    {
                        var changed = PropertyChanged;
                        if (changed == null)
                            return;
            
                        changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
                    }
                    #endregion
                    private string total;
                    public string Total
                    {
                        get { return total; }
                        set
                        {
                            total = value;
                            OnPropertyChanged();
                        }
                    }
            
                    private bool yearChecked;
                    public bool YearChecked
                    {
                        get { return yearChecked; }
                        set
                        {
                            yearChecked = value;
                            OnPropertyChange();
                        }
                    }
                    private bool monthChecked;
                    public bool MonthChecked
                    {
                        get { return monthChecked; }
                        set
                        {
                            monthChecked = value;
                            OnPropertyChange();
                        }
                    }
                    private bool weekChecked;
                    public bool WeekChecked
                    {
                        get { return weekChecked; }
                        set
                        {
                            weekChecked = value;
                            OnPropertyChange();
                        }
                    }
                    public string GetTotal()
                    {
                        if(YearChecked == true) { return "$10"; }
                        if(monthChecked == true) { return "$20"; }
                        if(weekChecked == true) { return "30"; }
                        else { return "$50"; }
                    }
                }
            }

In your constructor you are setting the backing field instead of the property, replace total by Total

public MainPageViewModel()
{
Total = GetTotal();
}

also you should call GetTotal() not only on the constructor but whenever one of the three properties changed:

                    private bool yearChecked;
                    public bool YearChecked
                    {
                        get { return yearChecked; }
                        set
                        {
                            yearChecked = value;
                            OnPropertyChange();
                            Total = GetTotal();
                        }
                    }
                    private bool monthChecked;
                    public bool MonthChecked
                    {
                        get { return monthChecked; }
                        set
                        {
                            monthChecked = value;
                            OnPropertyChange();
                            Total = GetTotal();
                        }
                    }
                    private bool weekChecked;
                    public bool WeekChecked
                    {
                        get { return weekChecked; }
                        set
                        {
                            weekChecked = value;
                            OnPropertyChange();
                            Total = GetTotal();
                        }
                    }
  • Also in your GetTotal() you should use properties in your if condition not their backing field: MonthChecked instead of monthChecked and WeekChecked instead of weekChecked .

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

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