Search code examples
uwplistviewitemtextblock

Can't put the TextBlock foreground in black UWP Color Foreground ListView


If leftclick I would like to put the TextBlock foreground in gray, and if rightclick I would like to put the TextBlock foreground in black. Actually program not do it when rightclick, but do it when leftclick. What's the problem about it ?

    private void SelectList1_LeftTapped(object sender, ItemClickEventArgs e)
    {

        dynamic clickedItem = e.ClickedItem;
        ListViewItem item = SelectList1.ContainerFromItem(clickedItem) as ListViewItem;
        item.Foreground = new SolidColorBrush(Colors.Gray);
        Article selectArticle = item.Content as Article;
        
        WebView1.Source = new System.Uri(selectArticle.article_link);
        DataAccess.ArticleIsRead(selectArticle.article_id);

    }


    private void SelectList1_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {

        ListViewItemPresenter clickedItem = e.OriginalSource as ListViewItemPresenter;
        Article selectArticle = clickedItem.DataContext as Article;
        ListViewItem item = SelectList1.ContainerFromItem(selectArticle) as ListViewItem;

        item.Foreground = new SolidColorBrush(Colors.Black);
        item.Background = new SolidColorBrush(Colors.Black);

        string selectArticleId = selectArticle.article_id;
        DataAccess.ArticleNotRead(selectArticleId);
    }

Solution

  • I tested my code by setting IsItemClickEnabled="True" with ListView, left-click and right-click could work well.

        <Border Grid.Column="1">
            <ListView x:Name="SelectList1" ItemsSource="{x:Bind allArticleList}" IsItemClickEnabled="True" ItemClick="SelectList1_LeftTapped" RightTapped="SelectList1_RightTapped" ItemTemplate="{StaticResource ArticleDataTemplate}" ItemContainerStyleSelector="{StaticResource ArticleStyleSelector}">
            </ListView>
        </Border>