Picker SelectedItem binding issue

Rask

Member
Joined
Mar 29, 2023
Messages
7
Programming Experience
5-10
I am running into an issue since I am not super familiar with mvvm and binding.

I have a picker in a collectionview. I want this bind to OnPropetyChaning. But since it is a collectionview all of the pickers are bind the same item list of option. So when I change one all of the are effected.

How do I bind each picker to an individual item in the items ObservableCollection?

I have tried sever solution buy making [ObservableProperty] with set => SetProperty(ref _testPossibleResult , value); etc.

Every solution I can think of or is suggested to me results in the same behaviour. So I need some help, is this my issue or is Maui with collectionview not able to provide the functionality I need?

viewmodel and model:
public partial class MainPageViewModel : ObservableObject
{

    public List<PossibleTestResultModel> l_PossibleTestResultModels { get; set; }
    public List<FailedTestReasonsModel> l_FailedTestResultModels { get; set; }

    public MainPageViewModel()
    {
        Items = new ObservableCollection<string>();
        l_PossibleTestResultModels = new List<PossibleTestResultModel>()
        {
            new PossibleTestResultModel() { testPossibleResult = "NA"},
            new PossibleTestResultModel() { testPossibleResult = "OK"},
            new PossibleTestResultModel() { testPossibleResult = "FAIL"},

        };

        l_FailedTestResultModels = new List<FailedTestReasonsModel>()
        {
            new FailedTestReasonsModel() { failedTestReason = "CABLE"},
            new FailedTestReasonsModel() { failedTestReason = "LINK"},
            new FailedTestReasonsModel() { failedTestReason = "MODBUS"},
            new FailedTestReasonsModel() { failedTestReason = "STICK"},
            new FailedTestReasonsModel() { failedTestReason = "OTHER"},
        };

    }

    [ObservableProperty]
    ObservableCollection<string> items;

    [ObservableProperty]
    string text;

    [ObservableProperty]
    bool isActive;
    
    [RelayCommand]
    void Add()
    {
        Items.Add(Text);
        // PossibleTestResults.Add(new TestResultModel() { testResult = text });
        Text = string.Empty;
    }

    [ObservableProperty]
    private PossibleTestResultModel possibleTestResultSelected;
    partial void OnPossibleTestResultSelectedChanging(PossibleTestResultModel possibleTestResultSelected)
    {
        IsActive = false;

        //TODO make it not static bind to a number. If the order changes the code will break
        if ("FAIL" == possibleTestResultSelected.testPossibleResult)
        {
            IsActive = true;
            Console.WriteLine(possibleTestResultSelected);
        }
    }
    [ObservableProperty]
    public int possibleTestFailureResultSelected;
    partial void OnPossibleTestFailureResultSelectedChanging(int possibleTestFailureResultSelected)
    {
        //TODO make it not static bind to a number. If the order changes the code will break
        if (1 == possibleTestFailureResultSelected)
        {
            Console.WriteLine(possibleTestFailureResultSelected);
        }
    }

}
public class PossibleTestResultModel
{
    public string testPossibleResult { get; set; }
}

public class FailedTestReasonsModel
{
    public string failedTestReason { get; set; }
}
view:
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="{x:Type x:String}">
                    <Grid ColumnDefinitions="150,150,150"
                          RowDefinitions="auto,auto,auto"
                          Padding="10"
                          Margin="10">
                        <Label Grid.Column="0"  Text="Test text"
                               Grid.Row="0">
                        </Label>
                        <Picker Grid.Column="1"
                                Grid.Row ="0" x:Name="testPicker" Title="Test Result"
                            ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainPageViewModel}}, Path = l_PossibleTestResultModels}"
                            ItemDisplayBinding ="{Binding testPossibleResult}"
                            SelectedItem="{Binding  Source={RelativeSource AncestorType={x:Type viewmodel:MainPageViewModel}}, Path = PossibleTestResultSelected}"
                            TextColor="Black">
                        </Picker>
                        <Picker Grid.Column="2"
                                Grid.Row ="0" x:Name="failPicker" Title="Fail Cause"
                            ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainPageViewModel}}, Path = l_FailedTestResultModels}"
                            ItemDisplayBinding ="{Binding failedTestReason}"
                                
                            TextColor="Black">
                        </Picker>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
 
Solution
I forgot to update this one.

the solution was add the picker list to the model.
Then when the model is populated, create a new temp list of strings, and add the result to the list of the mode.
I forgot to update this one.

the solution was add the picker list to the model.
Then when the model is populated, create a new temp list of strings, and add the result to the list of the mode.
 
Solution

Latest posts

Back
Top Bottom