xamarin 是否可以将ListView下的Picker的SelectedItem属性值存储在数组中?

wb1gzix0  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(191)

I am working with Xamarin ListView and Picker, trying to create an android app that calculates a student's GPA in one page(view). I have a class GPADetails that takes care of the Picker properties. This class contains a "List"...
public ObservableRangeCollection<string> UnitList{get;set;}
...of units binded to the ItemSource of the Picker. it also contains a "property field"...
private string selectedUnit=null;
public string SelectedUnit { get => selectedUnit;
set => SetProperty(ref selectedUnit, value); } ...that is binded to the SelectedItem property of the picker.
The ListView is being populated by binding a "List"...
public ObservableRangeCollection<GPADetails> GPADetailsList {get;set;}
...of multiple objects of type GPADetails class to the ItemSource of the ListView.(so that the user can pick different units for different subjects) Here's the function that populates the listView

public void DisplayTemplates()

        {

            GPADetailsList.Clear();

            //Instantiates  template object equivalent to the number of courses

            for (int i = 0; i < int.Parse(NumberOfCourses); i++)

            {

                GPADetailsList.Add(

                new GPADetails

                {

                    //initializing picker properties with picker items

                    UnitList = UnitItems,                    

                    SelectedUnit = null,                    

                    TemplateID = i,

                   

                });

                

            }

        }

Heres the Xaml of the ListView and the picker... ...

<ListView x:Name="listView" ItemsSource="{Binding GPADetailsList}" SelectionMode="None" >
            <ListView.ItemTemplate>

                <DataTemplate>

                    <ViewCell>

                            <StackLayout >

                                <Editor  Placeholder="Course Code" PlaceholderColor="White"                                        HorizontalOptions="StartAndExpand">
                                </Editor>

                                <Picker x:Name="PickerUnit" Title="Pick a Unit" TitleColor="white"

                                        HorizontalOptions="EndAndExpand"
        VerticalOptions="Fill" HorizontalTextAlignment="Center"                                        VerticalTextAlignment="Center" FontSize="15"
ItemsSource="{Binding UnitList}"
SelectedItem="{Binding SelectedUnit, Mode=TwoWay }"
                                      >
                             </Picker>
                          </StackLayout>                      

                    </ViewCell>

                </DataTemplate>

            </ListView.ItemTemplate>

        </ListView>

...
Now here's where I'm having problems. Each time the user selects a unit on the page, the selectedItem property of the picker is triggered. And the SelectedUnit property of the GPADetails class detects the property changed. I want so store the SelectedItem of each Picker that is under the ListView in an array. Using a property TemplateID of class GPADetails , I'm able to track which picker has been selected. So I use TemplateID as the index of my array. But I keep having problems because my C# is weak.
I tried doing this in the SelectedUnit property in class GPADetails by using a condition in the set accessor and initializing the array at the selected index with the selectedUnit . Heres the code..

private string selectedUnit;

        public string SelectedUnit

        {

            get { return selectedUnit; }

            set

            {

                SetProperty(ref selectedUnit, value);

                if (selectedUnit != null)

                    SelectedUnitList[TemplateID] = selectedUnit;

            }

 

        }

But with that, i can only assign one value to the array, if i try to assign another, the previously assigned index goes back to the default value, null.
P.S. I don't know if I asked this right, but any help would be appreciated, thanks dev fam...

zhte4eai

zhte4eai1#

我删除了我的原始帖子,下面是我的新答案。

DisplayTemplate中创建的每个示例都是独立的。这意味着每个示例都有不同的SelectedUnitList。这就是为什么只能在SelectedUnitList中设置一个选取器值的原因。

在这里我给予你一些建议。
在你的GPADetails.cs中,添加一个新的属性,那就是一个名为 SelectedUnitDict 的字典。(与 SelectedUnitList 相比,我更喜欢使用字典作为数组的索引,这容易引起麻烦):

public Dictionary<int,string> SelectedUnitDict { get; set; } //The key(int) is *TemplateID* and the value(string) is *SelectedItem*.

在您的GACalculationViewmodel中,也创建一个属性(为了方便起见,我使用相同的名称):

public Dictionary<int, string> SelelctedUnitDict { get; set; }

并在DisplayTemplates方法中添加一个新行,使GPADetails的每个示例在GPACalculationViewmodel中获得相同的 SelectedUnitDict

for (int i = 0; i < int.Parse(NumberOfCourses); i++)
    {

        GPADetailsList.Add(

            new GPADetails

            {

                //initializing picker properties with picker items

                UnitList = UnitItems,                    

                SelectedUnit = null,                    

                TemplateID = i,

                // Add this new line
                SelelctedUnitDict = SelelctedUnitDict

            });

最后,在GPADetails.cs中,就像您的代码一样:

public string SelectedUnit
    {
        ...
        set
        {
            ...
            if (selectedUnit != null)
            {
                
                SelectedUnitDict[TemplateID] = selectedUnit;
            }

        } 
    }

这对我很有效。我可以在GPACalculationViewmodelSelectedUnitDict 中获取每个选取器的selectedItem。键是 TemplateID,值是 SelectedItem
希望我的回答能对你有所帮助。

ecr0jaav

ecr0jaav2#

我认为您可以使用属性/事件与ListView交互,而不是依赖于可观察属性。
您可以使用ItemSelected

<ListView ItemsSource="{x:Static local:interactiveListViewXaml.items}" ItemSelected="OnSelection" ItemTapped="OnTap" IsPullToRefreshEnabled="true" Refreshing="OnRefresh">

https://github.com/xamarin/xamarin-forms-samples/blob/main/UserInterface/ListView/Interactivity/interactivityListView/interactivityListView/Views/interactiveListViewXaml.xaml
关于实现,我不确定TemplateID是什么,或者所有这句话I want so store the SelectedItem of each Picker under the ListView in an array using a property我不明白

相关问题