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