繁体   English   中英

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

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

到目前为止,我发现的所有 RadioButtons 示例都将值硬编码在页面的 xaml 中。 我正在寻找从数据库中提供字符串列表(目前)并遇到 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; }
    }
}

只要我对 ViewCell 中的 RadioButton 的 GroupName 进行硬编码,一切正常,但我最终会为页面上的每个单选按钮提供一个组名。 我尝试将属性添加到模板中的 StackLayout:

<StackLayout RadioButtonGroup.GroupName="someGroup">

并将其从 RadioButton 中取出,但是当我在 OnRadioChanged 中放置断点时,GroupName 为空。

我想使用 ChoiceViewModel 中的 Code 属性作为组名,但尚未得到正确的相对绑定。 感谢您对绑定或执行 DataTemplate 的不同方式的任何帮助。

您可以使用可绑定的 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