简体   繁体   English

Xamarin 的 RadioBox 列表从视图模型中设置 GroupName

[英]Xamarin list of RadioBox set GroupName from view model

All the examples for RadioButtons I've found so far have the values hard-coded in the xaml for the page.到目前为止,我发现的所有 RadioButtons 示例都将值硬编码在页面的 xaml 中。 I'm looking to feed a list of strings (for now) from a database and running into an issue with the GroupName.我正在寻找从数据库中提供字符串列表(目前)并遇到 GroupName 的问题。

My data template for a radio display type:

            <DataTemplate x:Key="RadioTemplate">
                <StackLayout>
                    <Label 
                        Text="{Binding Name}"
                        Style="{StaticResource LabelStyle}">
                    </Label>
                    <ListView ItemsSource="{Binding Choices.Choices}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <RadioButton Value="{Binding}"
                                            Content="{Binding}"
                                            GroupName="someGroupName"
                                            CheckedChanged="OnRadioChanged">
                                    </RadioButton>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackLayout>
            </DataTemplate>


MainPageViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Xamarin.Forms;
using FieldDataTemplateSelectorSample.Validations;

namespace FieldDataTemplateSelectorSample
{
    public class MainPageViewModel : ValidationViewModelBase
    {
        public MainPageViewModel()
        {
            _fields = new ObservableCollection<FieldViewModel>();
        }

        private ObservableCollection<FieldViewModel> _fields;
        public ObservableCollection<FieldViewModel> Fields
        {
            get { return _fields; }
            set
            {
                _fields = value;
                RaisePropertyChanged();
            }
        }

        private bool _showValidationSummary;
        public bool ShowValidationSummary
        {
            get { return _showValidationSummary; }
            set
            {
                _showValidationSummary = value;
                RaisePropertyChanged();
            }
        }

        ICommand _submitCommand;
        public ICommand SubmitCommand
        {
            get => _submitCommand ?? (_submitCommand = new Command(ValidateAndSave));
        }

        private void ValidateAndSave(object obj)
        {
            ValidateAll();
            if (ErrorStateManager.HasError)
            {
                ShowValidationSummary = true;
            }
            else
            {
                ShowValidationSummary = false;
            }
        }
    }
}

using System.Collections.Generic;
using Xamarin.Forms;
using FieldDataTemplateSelectorSample.Validations;


namespace FieldDataTemplateSelectorSample
{
    public class FieldViewModel : ValidationViewModelBase
    {
        public FieldViewModel()
        {
            _choices = new ChoiceViewModel();
        }

        private string _value;

        public long Id { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public string Code { get; set; }
        public string Value 
        { 
            get
            {
                return _value;
            }           
            set
            {
                _value = value;
                RaisePropertyChanged();
                Validate();
            }
        }
        public string PlaceholderText { get; set; }
        public int SortOrder { get; set; }
        public string DisplayType { get; set; }
        public bool IsReadOnly { get; set; }
        public bool IsEnabled { get; set; }

        private ChoiceViewModel _choices;
        public ChoiceViewModel Choices
        {
            get 
            { 
                return _choices; 
            }
            set 
            { 
                _choices = value; 
                RaisePropertyChanged(); 
            }
        }

        void OnChoicesRadioButtonCheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            // what else do we need to do?  Update value?
            
            RaisePropertyChanged();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace FieldDataTemplateSelectorSample
{
    public class ChoiceViewModel
    {
        public ChoiceViewModel()
        {
            Choices = new List<string>();
        }

        public string Code { get; set; }
        public List<string> Choices { get; set; }
    }
}

As long as I hard-code the GroupName for the RadioButton in the ViewCell, everything works but that I'll end up with one group name for every single radio button on the page.只要我对 ViewCell 中的 RadioButton 的 GroupName 进行硬编码,一切正常,但我最终会为页面上的每个单选按钮提供一个组名。 I've tried adding the property to the StackLayout in the template:我尝试将属性添加到模板中的 StackLayout:

<StackLayout RadioButtonGroup.GroupName="someGroup">

and taking it out out of the RadioButton but when I put a breakpoint in OnRadioChanged, the GroupName comes in as null.并将其从 RadioButton 中取出,但是当我在 OnRadioChanged 中放置断点时,GroupName 为空。

I'd like to use the Code property in the ChoiceViewModel as the group name but haven't gotten the relative binding correct yet.我想使用 ChoiceViewModel 中的 Code 属性作为组名,但尚未得到正确的相对绑定。 Any help with the binding or different way to do the DataTemplate is appreciated.感谢您对绑定或执行 DataTemplate 的不同方式的任何帮助。

you could use a bindable StackLayout which will give you more control over the containter您可以使用可绑定的 StackLayout ,它可以让您更好地控制容器

<StackLayout>
    <Label Text="{Binding Name}" Style="{StaticResource LabelStyle}" />
    <StackLayout RadioButtonGroup.GroupName="{Binding Name}"      
           BindableLayout.ItemsSource="{Binding Choices.Choices}" >
       <BindableLayout.ItemTemplate>
         <DataTemplate>
           <ViewCell>
               <RadioButton Value="{Binding}" Content="{Binding}"
                   CheckedChanged="OnRadioChanged" />
           </ViewCell>
         </DataTemplate>
      </BindableLayout.ItemTemplate>
  </StackLayout>
</StackLayout>

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

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