选择每个列表视图项并在通用应用中应用修改

本文关键字:应用 修改 列表 视图 选择 | 更新日期: 2023-09-27 18:33:56

我有一个显示这些信息的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 个列表视图项目,没有任何更改请提供任何帮助,如何更正我的代码感谢您的帮助

更新:I像这样使用了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));
                            }
                    }

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

选择每个列表视图项并在通用应用中应用修改

读了 2 遍后,我明白你想做什么了。您的代码逻辑错误。你不能在列表项中使用x:names,因为代码不知道女巫来说话。您必须使用绑定。

数据绑定概述

InotifyPropertyChanged.PropertyChanged

为列表项创建类

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;

要获取Buton上的项目 单击"

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 中显示的值,但我认为这样它会起作用,否则您可能必须更改数据类型。