Maui为什么RadioButton选中更改事件会取得错误的模型?

huangapple go评论106阅读模式
英文:

Maui why radiobutton checked changed event takes wrong model?

问题

以下是您要翻译的内容:

public class First
{
   public int Id {get; set;}
   public string Name {get; set;}
   public ICollection<First> Collection {get; set;}
}

public class Second
{
   public int Id {get; set;}
   public bool IsCorrect {get; set;}
   public int ParentId {get; set;}
   public First Parent {get; set;}   
}

我在我的视图模型中将ICollection<First>定义为ObservableCollection<First>。所以在我的UI中,我有一个CollectionView,其中的ItemSource是"First"类元素的集合,还有一个内嵌的CollectionView,用于"Second"类的集合。对于这个子集合,我有一个带有单选按钮的Item模板,就像这样:

<CollectionView ItemSource="{Binding ListOfFirst}">
  <CollectionView.ItemTemplate>
    <DataTemplate x:DataType="models:First">
     <Label Text="{Binding Name}"/&gt>
    
     <CollectionView ItemSource="{Binding Collection}">
       <CollectionView.ItemTemplate>
         <DataTemplate x:DataType="models:Second">
           <RadioButton/>
         </DataTemplate>
       </CollectionView.ItemTemplate>
     </CollectionView>
</CollectionView>

我为"Second"类的每个集合都有一个单选按钮组,如果我在我的UI和代码中定义事件到命令行为,就像这样:

<RadioButton  IsChecked="{Binding IsCorrect}" 
              GroupName="{Binding ParentId}">  
                                                                           
                            

  <RadioButton.Behaviors>
    <toolkit:EventToCommandBehavior EventName="CheckedChanged"
                                    Command="{Binding Source={x:Reference TestEditorPage}, Path=BindingContext.SpacesRadioButton_CheckedChangedCommand}"
                                    CommandParameter="{Binding .}"/>
  </RadioButton.Behaviors>
</RadioButton>
[RelayCommand]
public void SpacesRadioButton_CheckedChanged(Second second)
{
            
}

问题是,无论我点击哪个单选按钮组,我总是得到集合中的第一个元素。例如,如果我在列表中有3个"Second"类元素,无论我点击哪个组,我始终得到第一个元素。我还尝试过不是通过事件到命令行为而是通过常规事件来实现这一点。按钮的代码和事件代码如下:

<RadioButton  IsChecked="{Binding IsCorrect}" 
              GroupName="{Binding ParentId}" 
              CheckedChanged="RadioButton_CheckedChanged"/>       
private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
   var rb = (RadioButton)sender;
   var second = (Second)rb.BindingContext;
}

在这种情况下,情况会好一些,但仍然不正确。当我第一次点击按钮组时,我得到了正确的模型。但是当我第二次点击该组时,我得到了先前选择的模型。也就是说,同样的示例中有3个"Second"类元素的列表。当我第一次点击第二个元素时,我在事件代码中得到了它。但是当我决定点击第3个元素时,不知何故在事件代码中我得到了相同的第二个元素。例如,如果我想选择第一个元素,我却得到了第三个元素。谁能解释为什么会发生这种情况以及如何修复它?非常感谢您的帮助。

英文:

So i have something like this model

public class First
{
   public int Id {get; set;}
   public string Name {get; set;}
   public ICollection<First> Collection {get; set;}
}

public class Second
{
   public int Id {get; set;}
   public bool IsCorrect {get; set;}
   public int ParentId {get; set;}
   public First Parent {get; set;}   
}

I define ICollection<First> as ObservableCollection<First> inside my view model. So inside my UI i got collection view with item source of "First" class elements collection and also one more collection view inside for "Second" class collection. And for this child collection i got item template with radio button. So something like this

&lt;CollectionView ItemSource=&quot;{Binding ListOfFirst}&quot;&gt;
  &lt;CollectionView.ItemTemplate&gt;
    &lt;DataTemplate x:DataType=&quot;models:First&quot;&gt;
     &lt;Label Text=&quot;{Binding Name}&quot;/&gt;
    
     &lt;CollectionView ItemSource=&quot;{Binding Collection}&quot;&gt;
       &lt;CollectionView.ItemTemplate&gt;
         &lt;DataTemplate x:DataType=&quot;models:Second&quot;&gt;
           &lt;RadioButton/&gt;
         &lt;/DataTemplate&gt;
       &lt;/CollectionView.ItemTemplate&gt;
     &lt;/CollectionView&gt;
&lt;/CollectionView&gt;

I got radiobutton group for each collection of "Second" class and if i define event to command behavior inside my ui and code like this

&lt;RadioButton  IsChecked=&quot;{Binding IsCorrect}&quot; 
              GroupName=&quot;{Binding ParentId}&quot;&gt;  
                                                                           
                            
  &lt;RadioButton.Behaviors&gt;
    &lt;toolkit:EventToCommandBehavior EventName=&quot;CheckedChanged&quot;
                                    Command=&quot;{Binding Source={x:Reference TestEditorPage}, Path=BindingContext.SpacesRadioButton_CheckedChangedCommand}&quot;
                                    CommandParameter=&quot;{Binding .}&quot;/&gt;
  &lt;/RadioButton.Behaviors&gt;
&lt;/RadioButton&gt;
[RelayCommand]
public void SpacesRadioButton_CheckedChanged(Second second)
{
            
}

The problem is that no matter which group of radio buttons I click on, I always get the very first element of the collection in the team. For example, if I have 3 elements of class "Second" in the list, I always get the first element. I also tried to do this not through the event to command behavior but through a regular event. The code of the button and the event code is as follows

&lt;RadioButton  IsChecked=&quot;{Binding IsCorrect}&quot; 
              GroupName=&quot;{Binding ParentId}&quot; 
              CheckedChanged=&quot;RadioButton_CheckedChanged&quot;/&gt;       
private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
   var rb = (RadioButton)sender;
   var second = (Second)rb.BindingContext;
}

In this case, the situation is better, but still incorrect. When I click on a group of buttons for the first time, I get the correct model. But when I click on the group for the second time, I get exactly the model of the previous element that was selected. That is, the same example with a list of 3 elements of the "Second" class. When I click on the second element for the first time, I get it in the event code. When I decide to click on the 3rd element, for some reason I get the same second element in the event code. And for example, if I want to select the first element, I get the 3rd element. Who can explain why this happens and how to fix it? I will be very grateful for your help

答案1

得分: 0

当您选择一个单选按钮并单击另一个按钮时,会发生两种更改:

  • 先前选中的按钮变为未选中。
  • 单击的按钮变为选中。

因此,RadioButton_CheckedChanged 会被调用两次。

if (e.Value) ...
英文:

When you have one radio button selected, and click another one, TWO changes happen:

  • The previous checked button becomes UNCHECKED (unselected).
  • The clicked button becomes CHECKED (selected).

Thus, RadioButton_CheckedChanged is called TWICE.

if (e.Value) ...

huangapple
  • 本文由 发表于 2023年7月20日 17:57:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定