【问题标题】:Get Selected Value from SfRadioButton in xamarin MVVM从 xamarin MVVM 中的 SfRadioButton 获取选定值
【发布时间】:2021-08-18 22:33:46
【问题描述】:

我想使用 syncfusion 单选按钮在我的 viewModel 中获取所选单选按钮的值,以便我可以根据所选值执行一些逻辑。我似乎无法获得选定的值。请帮忙!

Xaml

<syncfusion:SfRadioGroup x:Name="radioGroup"
      BindableLayout.ItemsSource="{Binding Items}">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <syncfusion:SfRadioButton 
                      Text="{Binding PaymentMethod}" />
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </syncfusion:SfRadioGroup>

查看模型

 public List<PaymentMthd> Items { get; set; }



Items = new List<PaymentMthd>()

{

new PaymentMthd(){PaymentMethod = "Cash" , id = 1},

new PaymentMthd(){PaymentMethod = "Visa Card" , id = 2},



};

private int _IsChecked;



public int IsChecked

{

get { return _IsChecked; }

set { SetProperty(ref _IsChecked, value); }

【问题讨论】:

标签: xamarin mvvm radio-button syncfusion


【解决方案1】:

您可以添加StateChanged 事件来监听状态。

<syncfusion:SfRadioButton 
                  Text="{Binding PaymentMethod}" StateChanged="RadioButton_StateChanged" />

然后在你的代码后面:

private void RadioButton_StateChanged(object sender, StateChangedEventArgs e)
{
  if (e.IsChecked.HasValue && e.IsChecked.Value)
  {
     //if checked,do something
  }
  else if (e.IsChecked.HasValue && !e.IsChecked.Value)
  {
    //if not checked,do something
  }
}

您还可以使用CheckedChanged 事件来获取 RadioGroup 中选定的 RadioButton,该事件在 RadioGroup 中更改选择时已被调用。

<syncfusion:SfRadioGroup x:Name="radioGroup" BindableLayout.ItemsSource="{Binding Items}"
               CheckedChanged="CheckedChanged_event">
  <BindableLayout.ItemTemplate>
            <DataTemplate>
                <syncfusion:SfRadioButton Text="{Binding PaymentMethod}" />
            </DataTemplate>
  </BindableLayout.ItemTemplate>
</syncfusion:SfRadioGroup>

在你的代码后面:

 private void CheckedChanged_event(object sender,Syncfusion.XForms.Buttons.CheckedChangedEventArgs e)
 { 
   // To get the selected RadioButton - e.CurrentItem
  // To get the previously selected RadioButton -  e.PreviousItem
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2018-03-29
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多