Search code examples
listviewwindows-10listviewitemwindows-10-universalwindows-10-mobile

Changing Forecolor of ListView


I really need your help, because I couldn't find a solution for my problem!

I'm developing a Universal Windows 10 app and have a timetable (ListView) in it.

Now I'd like to mark the item in the timetable, which is currently running. For Example there is an item which goes from Monday 07:00 to 13:00 and now it is Monday 12:00 so the item should change its color to red (or something) like it is marked.

I've tried several things now and that's the code so far:

First here is my xaml-Code for the ListView:

<RelativePanel Name="rpStundenplan">
        <ListView x:Name="lstViewStundenplan" HorizontalContentAlignment="Stretch" SelectionChanged="lstViewStundenplan_SelectionChanged">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <RelativePanel Grid.Row="0" Grid.Column="0">
                            <RelativePanel Name="rpTag" Width="100">
                                <TextBlock Name="txtTag" FontSize="16" Text="{Binding Tag_lang}" TextWrapping="Wrap" RelativePanel.AlignHorizontalCenterWithPanel="True" />
                            </RelativePanel>
                            <RelativePanel Name="rpZeit" Width="auto" RelativePanel.Below="rpTag" RelativePanel.AlignHorizontalCenterWith="rpTag">
                                <TextBlock Name="txtZeitVon" FontSize="14" Text="{Binding Beginn}" VerticalAlignment="Center"/>
                                <TextBlock Name="txtZeitBis" FontSize="14" Text="{Binding Ende}" VerticalAlignment="Center" RelativePanel.RightOf="txtZeitVon" Margin="5,0,0,0" />
                            </RelativePanel>
                      </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </RelativePanel>

I'm filling the ListView with this code:

    private void StundenplanAktualisieren()
    {
        //Studiengänge aus integrierter Datenbank in ListView eintragen
        using (SQLite.Net.SQLiteConnection connIntern = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sPathdatabaseIntern))
        {
            var query = connIntern.Table<Classes.Stundenplan>();

            //ListBox erste leeren
            this.lstViewStundenplan.Items.Clear();

            foreach (var item in query)
            {
                this.lstViewStundenplan.Items.Add(item);
            }
        }
    }

And now the code, where the error appears:

public void AktuelleZeit()
    {
        //markiert die aktive Vorlesung
        ListView lstView = new ListView();
        lstView = lstViewStundenplan;

        for (int i = 1; i < lstView.Items.Count; i++)
        {
                    ListViewItem Item = (ListViewItem)lstViewStundenplan.Items[i];

                    Item.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
        }

    }

Unfortunately the line

ListViewItem Item = (ListViewItem)lstViewStundenplan.Items[i];

throws the exeption:

{"Unable to cast object of type 'CampusAppHSHof.Classes.Stundenplan' to type 'Windows.UI.Xaml.Controls.ListViewItem'."}

I hope the code is enough for you to reproduce the problem... I tried to make it as short as possible...


Solution

  • Usually we don’t manipulate the ListView’s item directly, the common way is adding the items to the ObservableCollection and set ItemSource property of ListView to bind to the ObservableCollection.

    According to the question, the lstViewStundenplan.Items gets the collection used to generate the content of the ListView. In your project, the item in collection is Stundenplan type. So you get the exeption:

    {"Unable to cast object of type 'CampusAppHSHof.Classes.Stundenplan' to type 'Windows.UI.Xaml.Controls.ListViewItem'."}

    You can use ItemsControl.ContainerFromItem to get the container corresponding to the specified item. That you can cast it to ListViewItem type.

    By the way, in AktuelleZeit method the code ListView lstView = new ListView(); lstView = lstViewStundenplan; semms it is redundant.

    For example:

    public void AktuelleZeit()
    {
        for (int i = 1; i < lstViewStundenplan.Items.Count; i++)
        {
            ListViewItem Item = (lstViewStundenplan).ContainerFromItem(lstViewStundenplan.Items[i]) as ListViewItem;
            Item.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
        }
    }