【问题标题】:RadioButtons binding on Windows Phone 8.1Windows Phone 8.1 上的 RadioButtons 绑定
【发布时间】:2015-08-25 14:51:36
【问题描述】:

我在绑定RadioButton 属性IsChecked 时遇到了一些问题。我在网格上有两个RadioButtonVisibility 绑定到我的视图模型上的一个属性。我想要实现的是始终将第一个 RadioButton 设置为 Checked 状态,当网格变得可见时。 这是一些代码:

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

按照我的逻辑,当属性State 进入特定状态时,它应该首先将RadioButton 设置为已检查,当网格变为可见时。它工作正常,直到我达到第二个RadioButton。然后我的绑定不起作用,当State 发生变化时,我的StateToBooleanConverter 没有任何反应。 我阅读了很多关于单选按钮绑定问题的信息,但在我的情况下没有任何效果。 如果没有用于检查 radioButton 的新属性,是否可以这样做?我将不胜感激任何建议我如何解决这个问题。

编辑:

IsChecked 有一些来自 viewmodel 和 Converter 的代码:

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

【问题讨论】:

  • 请附上你的cs代码。
  • State 是否实现了INotifyPropertyChanged
  • @mike-eason 是的,确实如此。
  • @lloyd 查看我的帖子,我添加了一些代码
  • 如何工作 base.RaisePropertyChanged(); ??

标签: c# xaml mvvm radio-button windows-phone-8.1


【解决方案1】:

假设 PageState 是一个枚举,this answer is what you're looking for.

您想要组合在一起的所有单选按钮都绑定到 ViewModel 的相同属性并且都使用相同的 ValueConverter。触发单选按钮选中/取消选中的值被传递到 ValueConverter 的 parameter 属性中。

对于您的具体问题,EnumBooleanConverter 可以直接复制粘贴到您的代码中(请务必阅读并确保您理解它)。

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>

【讨论】:

  • 谢谢。你快到了!几乎没有改进,我得到了我想要的。
猜你喜欢
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多