select 每个 ListViewItem 并在通用 App 中应用修改

select each ListViewItem and apply a modification in a universal App

我有一个显示这些信息的 ListView,我想要的是为我的按钮显示不同的背景如果我得到 fav = 1,如果我得到 aime = 1 则显示不同的图像: JSON格式:

{
success: 1,
total: 2,
locals: [
{
id_local: "82",
fav: 0,
aime: 0,
aimepas: 0,
all_like: "2",
all_dislike: "0",
},
{
id_local: "83",
fav: 1,
aime: 1,
aimepas: 0,
all_like: "5",
all_dislike: "0",
}
]
}

这是我的代码:

            Uri = "URL";
            var http = new HttpClient();
            http.MaxResponseContentBufferSize = Int32.MaxValue;
            var response = await http.GetStringAsync(Uri);
            var rootObject = JsonConvert.DeserializeObject<Project.Models.RootObject>(response);

            listme.ItemsSource = rootObject.locals;


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

                    if (rootObject1.locals[i].fav == 1)
                    {
                        m_button.Background = new SolidColorBrush(Color.FromArgb(255, 251, 187, 9)); //color1 :Yellow
                    }

                    else
                    {
                        m_button.Background = new SolidColorBrush(Color.FromArgb(255, 178, 178, 178));//color2 :Gray
                    }

                    if (rootObject1.locals[i].aime == 1)
                    {
                        likeimage.Source = new BitmapImage(new Uri("ms-appx:///images/coueur_rouge.png", UriKind.Absolute)); //image1
                    }

                    else
                    {
                        likeimage.Source = new BitmapImage(new Uri("ms-appx:///images/like.png", UriKind.Absolute)); //image2
                    }

                }

这是我的 xaml:

<ListView  x:Name="listme">
 <ListView.ItemTemplate >
   <DataTemplate >
     <Grid>
       ...
      <Button Background="Gray"  x:Name="m_button"/>
      <Button  Background="Gray" >
           <Image Source="images/like.png" x:Name="likeimage"/>
        </Button>
     </Grid>
   </DataTemplate >
 </ListView.ItemTemplate >
</ListView >

我得到的是 2 个列表视图项,没有任何更改 请帮助我如何更正我的代码 感谢帮助

更新:我已经使用了foreach,就像这样,但是我仍然遇到ListViewItems的问题:

listme.ItemsSource = rootObject1.locals;

                    foreach (var item in listme.Items.Cast<Locals>())
                    {

                        if (item.fav == 1)
                            {
                                m_button.Background = new SolidColorBrush(Color.FromArgb(255, 251, 187, 9)); //jaune
                            }

                            else
                            {
                                m_button.Background = new SolidColorBrush(Color.FromArgb(255, 178, 178, 178));//gris
                            }

                            if (item.aime == 1)
                            {
                                likeimage.Source = new BitmapImage(new Uri("ms-appx:///images/coueur_rouge.png", UriKind.Absolute));
                            }

                            else
                            {
                                likeimage.Source = new BitmapImage(new Uri("ms-appx:///images/like.png", UriKind.Absolute));
                            }

                    }

例如,当我 select 索引 =0 的项目中的按钮时,索引 =1 的项目将被修改,我不知道为什么会得到这个结果!! >_<

看了两遍我明白你想做什么了。你的代码逻辑是错误的。您不能在 listItems 中使用 x:names,因为代码不知道要与 进行对话。 你必须使用绑定。

Data binding overview

INotifyPropertyChanged.PropertyChanged

为列表项创建一个Class

public class Item : INotifyPropertyChanged
{
    private Uri thumbnail;
    public Uri Thumbnail
    {
        get { return thumbnail; }
        set
        {
            thumbnail = value;
            NotifyPropertyChanged("Thumbnail");
        }
    }

    private SolidColorBrush buttonColor = new SolidColorBrush(Color.FromArgb(255, 178, 178, 178));
    public SolidColorBrush ButtonColor
    {
        get { return buttonColor; }
        set
        {
            buttonColor = value;
            NotifyPropertyChanged("ButtonColor");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }
}

XAML

<ListView  x:Name="listme">
 <ListView.ItemTemplate >
   <DataTemplate >
     <Grid>
       ...
      <Button Background="{Binding ButtonColor} Tapped="Button_Tapped"/>
      <Button  Background="Gray" >
           <Image Source="{Binding Thumbnail}"/>
        </Button>
     </Grid>
   </DataTemplate >
 </ListView.ItemTemplate >
</ListView >

然后加载你这样的项目

//This one outside
        ObservableCollection<Item> Locals = new ObservableCollection<Item>();

    //in method or constractor
    //foreach
    Item listItem = new Item();
    listItem.Thumbnail = new Uri("ms-appx:///images/coueur_rouge.png", UriKind.Absolute);
    listItem.ButtonColor = new SolidColorBrush(Color.FromArgb(255, 251, 187, 9));
    Locals.Add(listItem);
    //end foreach

    // then do 
    listme.ItemsSource = Locals;

点击按钮获取物品

private void Button_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Item selectedItem = ((FrameworkElement)sender).DataContext as Item;
            SelectedItem.Thumbnail = null;//whatever you like
            SelectedItem.ButtonColor = null;//whatever you like
        }

您可能需要使用 IValueConverter 来显示 xaml 中的值,但我认为这种方式会起作用,否则您可能需要更改数据类型。