getting error on picker when binding data CommunityToolkit.Mvvm

Bhuwan 781 Reputation points
2025-02-10T10:21:01.89+00:00
 xmlns:viewModels="clr-namespace:Dev.ViewModels"
 xmlns:models="clr-namespace:Dev.Models"
 x:DataType="viewModels:LoginViewModel" 


<Picker ItemsSource="{Binding Genders,Mode=TwoWay}"
                        SelectedItem="{Binding SelectedItemGender,Mode=TwoWay}"
                        ItemDisplayBinding="{Binding PickerText}"
                        FontFamily="OpenSansSemibold"
                        VerticalOptions="CenterAndExpand" 
                        HorizontalOptions="FillAndExpand"
                        FontSize="12"/>
    public class PickerRoot
    {
        public string? PickerValue { get; set; }
        public string? PickerText { get; set; }
    }

internal partial class LoginViewModel
{
    [ObservableProperty]
    private ObservableCollection<PickerRoot> genders = new();
    [ObservableProperty]
    private PickerRoot? selectedItemGender;
    public LoginViewModel()
    {
        GetGenderList();
    }
    private void GetGenderList()
    {
        Genders.Clear();
        Genders.Add(new PickerRoot { PickerText = "Select", PickerValue = "Select" });
        Genders.Add(new PickerRoot { PickerText = "Male", PickerValue = "Male" });
        Genders.Add(new PickerRoot { PickerText = "Female", PickerValue = "Female" });
        SelectedItemGender = Genders.Where(x => x.PickerText == "Male").FirstOrDefault();
    }
}

getting error- XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified.

 <Picker ItemsSource="{Binding Genders}"
                        x:DataType="models:PickerRoot"
                        SelectedItem="{Binding SelectedItemGender,Mode=TwoWay}"
                        ItemDisplayBinding="{Binding PickerText}"
                        TitleColor="{DynamicResource LabelPlaceHolder}" 
                        FontFamily="OpenSansSemibold"
                        VerticalOptions="CenterAndExpand" 
                        HorizontalOptions="FillAndExpand"
                        FontSize="12"/>

if i specify x:DataType then getting below error
XamlC warning XC0045: Binding: Property "Genders" not found on "Dev.Models.PickerRoot".

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,914 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 35,351 Reputation points Microsoft Vendor
    2025-02-11T07:51:51.08+00:00

    Hello,

    Please modify the code like the following:

    <Picker ...
    <!-- x:DataType="models:PickerRoot" // remove this-->
                 ItemDisplayBinding="{Binding PickerText, x:DataType = models:PickerRoot}"
    >
    

    You got this error because Genders is a property of LoginViewModel and you have set x:DataType="viewModels:LoginViewModel" in the Page. At the meantime, PickerText is a property of models:PickerRoot

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.