0

我有一个带有绑定到国家列表的控制模板的单选按钮,一个国家有它的标志图像和名称。当我设置绑定时,元素的数量正确显示,但国家名称和国旗图像没有出现。我认为绑定在单选按钮的内容中不起作用。

 <!-- Control Template -->



<ContentPage.Resources>
         <ResourceDictionary>
             <ControlTemplate x:Key="ThemeRadioTemplate">
                 <Frame x:Name="CheckFrame" Padding="15,0,15,0" BackgroundColor="{StaticResource LightSecondoryColor}" HasShadow="False" HeightRequest="50" WidthRequest="240" HorizontalOptions="Center" VerticalOptions="Start" CornerRadius="30">
                     <Grid Margin="4" WidthRequest="80">
                         <Grid HeightRequest="20" HorizontalOptions="Start" VerticalOptions="Center" WidthRequest="20">
                             <Ellipse Fill="White" HeightRequest="18" HorizontalOptions="Center" Stroke="#140D38" StrokeThickness="1" VerticalOptions="Center" WidthRequest="18" />
                             <Ellipse x:Name="Check" BackgroundColor="Transparent" Fill="{StaticResource SecondoryColor}" HeightRequest="10" HorizontalOptions="Center" Stroke="#00E4B4" StrokeThickness="0" VerticalOptions="Center" WidthRequest="10" />
                         </Grid>
                         <!-- This enables us to put in dynamic content -->
                         <ContentPresenter />
                     </Grid>
                     <VisualStateManager.VisualStateGroups>
                         <VisualStateGroupList>
                             <VisualStateGroup x:Name="CheckedStates">
                                 <VisualState x:Name="Checked">
                                     <VisualState.Setters>
                                         <Setter TargetName="Check" Property="Opacity" Value="1" />
                                         <Setter TargetName="CheckFrame" Property="BackgroundColor" Value="{StaticResource LightSecondoryColor}" />
                                     </VisualState.Setters>
                                 </VisualState>
                                 <VisualState x:Name="Unchecked">
                                     <VisualState.Setters>
                                         <Setter TargetName="Check" Property="Opacity" Value="0" />
                                         <Setter TargetName="CheckFrame" Property="BackgroundColor" Value="{StaticResource ColorOnDarkBackground}" />
                                     </VisualState.Setters>
                                 </VisualState>
                             </VisualStateGroup>
                         </VisualStateGroupList>
                     </VisualStateManager.VisualStateGroups>
                 </Frame>
             </ControlTemplate>
         </ResourceDictionary>
     </ContentPage.Resources>
<StackLayout x:Name="stack" VerticalOptions="CenterAndExpand" Margin="0,20,0,20" BindableLayout.ItemsSource="{Binding CountryList}">
             <BindableLayout.ItemTemplate>
                 <DataTemplate>
                     <RadioButton ControlTemplate="{StaticResource ThemeRadioTemplate}" IsChecked="{Binding IsChecked}">
                         <RadioButton.Content>
                             <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Center" Spacing="20">
                                 <Image Source="{Binding FlagImage}" HeightRequest="32" HorizontalOptions="Start" VerticalOptions="Center"/>
                                 <Label Text="{Binding CountryName}" FontSize="Default" FontFamily="SemiBold" VerticalOptions="Center" TextColor="{StaticResource TextColor}" Margin="{OnPlatform Android='0,0,0,-7', iOS='0'}"/>
                             </StackLayout>
                         </RadioButton.Content>
                     </RadioButton>
                 </DataTemplate>
             </BindableLayout.ItemTemplate>
         </StackLayout>

屏幕截图显示国家名称和国旗图像没有出现,因为绑定不起作用

4

1 回答 1

0

从文档中显示任意内容,我们知道:

在 Android 上,RadioButton 对象将显示已设置为内容的 View 对象的基于字符串的表示形式。

因此,我们可以使用“内容”属性作为 XML 属性:

 <RadioButton ControlTemplate="{StaticResource RadioButtonTemplate}" IsChecked="{Binding IsChecked}" Content="{Binding CountryName}">

还有一个问题,你可以在这里跟进:https ://github.com/xamarin/Xamarin.Forms/issues/15176 。

于 2022-03-04T09:37:50.717 回答