简体   繁体   中英

Windows Phone C# Iterate through ListView child controls

I'm having a problem with a ListView for Windows Phone: I'm using a databinding to display all items in a folder, but when I try to iterate through them, I don't get a control, but the class I use to get the items in the LocalFolder of the app (The subjectpicker class)!

SubjectPicker class:

class SubjectPicker {
    public static async Task<List<SubjectPicker>> SubjectData() {
        List<string> Names = new List<string>();
        List<Brush> Colors = new List<Brush>();
        List<string> Items = new List<string>();
        List<List<TestOrTask>> TestsAndTasks = new List<List<TestOrTask>>();

        StorageFolder LocalFolder = ApplicationData.Current.LocalFolder;
        var files = await LocalFolder.GetFilesAsync();
        foreach (var file in files) {
            //await file.DeleteAsync();
            List<TestOrTask> TAT = new List<TestOrTask>();

            Names.Add(file.DisplayName);

            bool readItems = false;
            var contents = await FileIO.ReadLinesAsync(file);
            foreach (var content in contents) {
                if (content.Contains("Subject Color")) {
                    string colorname = content.Replace("Subject Color: ", "");
                    Colors.Add(ColorConverter.fromString_Brush(colorname));
                }
                else if (content == "<Items>") readItems = true;
                else if (content == "</Items>") readItems = false;

                if (readItems == true && content != "") {
                    // Layout: <Name>: <Score>/<Maximum>|<YYYY>/<MM>/<DD> 
                    string name = content.Split(':')[0];
                    string Score = content.Split(':')[1].Replace(" ", "");
                    string Date = content.Split('|')[1];
                    TestOrTask TOT = new TestOrTask(
                        name,
                        double.Parse(Score.Split('/')[0]),
                        double.Parse(Score.Split('/')[1]),
                        new DateTime(
                            Int32.Parse(Date.Split('/')[0]),
                            Int32.Parse(Date.Split('/')[1]),
                            Int32.Parse(Date.Split('/')[2])));
                    TAT.Add(TOT);
                }
            }
            Items.Add("Aantal Testen & Taken: " + TAT.Count.ToString());
            TestsAndTasks.Add(TAT);
        }

        var data = new List<SubjectPicker>();
        for (int i = 0; i < Names.Count; i++) {
            data.Add(new SubjectPicker(Names[i], Colors[i], Items[i], TestsAndTasks[i]));
        }

        return data;
    }

    public SubjectPicker(string name, Brush color, string itemstotal, List<TestOrTask> TestsAndTasks) {
        PickerName = name;
        PickerColor = color;
        ItemTotal = itemstotal;
        this.TestsAndTasks = TestsAndTasks;
    }

    public string PickerName { get; set; }
    public Brush PickerColor { get; set; }
    public string ItemTotal { get; set; }
    public List<TestOrTask> TestsAndTasks = new List<TestOrTask>();
}

Xaml Code:

<Page.Resources>
    <DataTemplate x:Key="SubjectTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Border Margin="0,9.5,0,0" Background="Transparent" >
                <Rectangle  Height="50" Width="50" Fill="{Binding PickerColor}" RadiusX="5" RadiusY="5"/>
            </Border>
            <StackPanel Grid.Column="1" Margin="14.5,0,0,0">
                <TextBlock Text="{Binding PickerName}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
                <TextBlock Text="{Binding ItemTotal}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}" />
            </StackPanel>
        </Grid>
    </DataTemplate>
</Page.Resources>

<Page.BottomAppBar>
    <CommandBar x:Name="AppBar" Visibility="Collapsed">
        <AppBarButton x:Name="EditSubject" Icon="Edit" Label="Aanpassen" Click="EditSubject_Click"/>
        <AppBarButton x:Name="DeleteSubject" Icon="Delete" Label="Verwijderen" Click="DeleteSubject_Click"/>
    </CommandBar>
</Page.BottomAppBar>

<Grid x:Name="MainGrid" Loaded="MainGrid_Loaded">
    <Controls:TopBarControl x:Name="TopBarControl" Margin="0" VerticalAlignment="Top" PageName="Vakken" ControlsVisible="All" Width="Auto" BackButtonClicked="TopBar_BackButtonClicked" AddButtonClicked="TopBar_AddButtonClicked" EditButtonClicked="TopBar_EditButtonClicked"/>
    <Grid x:Name="ControlsGrid" Margin="0,50,0,-60" Tapped="ControlsGrid_Tapped">
        <ListView x:Name="SubjectList" ItemsSource="{Binding}" ItemTemplate="{StaticResource SubjectTemplate}"/>
    </Grid>
</Grid>

Void to iterate through:

private async Task SelectSubjects() {
    for (int i = 0; i < SubjectList.Items.Count; i++) {
        var control = SubjectList.Items[i];
        Grid subject = control as Grid;
        if (subject != null) {
            subject.Background = new SolidColorBrush(SchoolToolsColors.AppColor);
            await Task.Delay(TimeSpan.FromMilliseconds(50));
        }
    }
    isSelecting = true;
    AppBarVisible = Visibility.Visible;
}

Thanks in advance!!!

When iterating on databinded control you'll always get its underlying data.

You should Style.Triggers to alter UI based on data (changing background, showing/hiding controls, etc.).

However, there is a way to go with altering UI from C# but that would make XAML and code-behind tightly coupled => that introduces more complexity into your code - simply believe me, it is not what you want/need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM