简体   繁体   English

Windows Phone 8.1上的RadioButtons绑定

[英]RadioButtons binding on Windows Phone 8.1

I have some problem with binding my RadioButton property IsChecked . 我在绑定我的RadioButton属性IsChecked一些问题。 I have two RadioButton 's on the grid, which Visibility binded to a property on my viewmodel. 我在网格上有两个RadioButton ,这些Visibility绑定到了我的视图模型上的一个属性。 What i want to achieve is to always setting first RadioButton to Checked state, when grid is become visible. 我要实现的是,当网格变得可见时,始终将第一个RadioButton设置为Checked状态。 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. 按照我的逻辑,当属性State变为特定状态且网格变得可见时,应将第一个RadioButton设置为“已检查”。 And its working fine until i hit second RadioButton . 在我按下第二个RadioButton之前它一直工作正常。 Then my binding is not working and when State is changing nothing happens in my StateToBooleanConverter . 然后我的绑定不起作用,并且当State更改时, 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? 如果没有用于检查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 : viewmodel和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. 假设PageState是一个枚举,那么此答案就是您要寻找的。

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. 您希望组合在一起的所有单选按钮都绑定到ViewModel的相同属性,并且都使用相同的ValueConverter。 The value that triggers a radio button check / uncheck is passed into the ValueConverter's parameter property. 触发单选按钮选中/取消选中的值将传递到ValueConverter的parameter属性中。

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). 对于您的特定问题,可以将EnumBooleanConverter直接复制粘贴到您的代码中(一定要EnumBooleanConverter阅读并确保您理解它)。

The XAML then becomes 然后,XAML变为

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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