简体   繁体   中英

RadioButtons binding on Windows Phone 8.1

I have some problem with binding my RadioButton property IsChecked . I have two RadioButton 's on the grid, which Visibility binded to a property on my viewmodel. What i want to achieve is to always setting first RadioButton to Checked state, when grid is become visible. Here is some code:

<Grid Visibility="{Binding State, Converter={StaticResource VisibilityConverter}}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>


                <RadioButton Grid.Row="0"
                             Margin="20,0" 
                             IsChecked="{Binding State, Converter={StaticResource StateToBooleanConverter}}"
                             Content="content 1" />

                <RadioButton Grid.Row="1"
                             Margin="20,0"
                             Content="content 2" />


            </Grid>

Following my logic it should set first RadioButton as Checked when property State is going to specific state, when grid is become visible. And its working fine until i hit second RadioButton . Then my binding is not working and when State is changing nothing happens in my StateToBooleanConverter . I read a lot of information about problems with binding in radiobuttons, but nothing worked in my case. Is it possible do it without new property for checking radioButton? I would be appreciated for any advise how i can fix this issue.

Edit:

There is some code from viewmodel and Converter for IsChecked :

public class MainViewModel : ViewModel
{
    public MainViewModel
    {
        this.ChangeState = new RelayCommand(this.ChangeStateExecute);
    }

    public PageState State
    {
        get
        {
            return this.state;
        }
        set
        {
            if (this.state != value)
            {
                this.state = value;
                base.RaisePropertyChanged();
            }
        }
    }

    public RelayCommand ChangeState { get; private set; }

    private void ChangeStateExecute()
    {
        this.State = PageState.RadioButtonsVisible;
    }
}

public class StateToBooleanConverter : Converter
{
    protected override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var state = (PageState)value;
        var result = state == PageState.RadioButtonsVisible;
        return result;
    }
}

Assuming that PageState is an enum, this answer is what you're looking for.

All of the radio buttons that you want to group together all bind to the same property of the ViewModel and all use the same ValueConverter. The value that triggers a radio button check / uncheck is passed into the ValueConverter's parameter property.

For your specific problem, the EnumBooleanConverter can be directly copy-pasted into your code (be sure to read though it and make sure you understand it).

The XAML then becomes

<Grid Visibility="{Binding State, Converter={StaticResource VisibilityConverter}}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>


            <RadioButton Grid.Row="0"
                         Margin="20,0" 
                         IsChecked="{Binding State, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=RadioButtonVisible}"
                         Content="content 1" />

            <RadioButton Grid.Row="1"
                         Margin="20,0"
                         IsChecked="{Binding State, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=*Insert enum value here*}"
                         Content="content 2" />


        </Grid>

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