简体   繁体   English

Windows Phone C#遍历ListView子控件

[英]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)! 我在使用Windows Phone的ListView时遇到问题:我正在使用数据绑定来显示文件夹中的所有项目,但是当我尝试遍历它们时,我没有控件,但是我使用的类是在应用程序的LocalFolder中获取项目(subjectpicker类)!

SubjectPicker class: SubjectPicker类:

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: Xaml代码:

<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.). 您应该使用Style.Triggers来基于数据(更改背景,显示/隐藏控件等)来更改UI。

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. 但是,有一种方法可以从C#更改UI,但这会使XAML和代码隐藏紧密耦合=>,从而在您的代码中引入更多的复杂性-简单地相信我,这不是您想要/不需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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