select each ListViewItem and apply a modification in a universal App

隐身守侯 提交于 2019-12-13 01:40:21

问题


I have a ListView that displays those informations,what I want is to display a different background for my button If I get fav = 1,and a different image if I get aime = 1,: JSON format:

{
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",
}
]
}

and this is my code:

            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
                    }

                }

this is my 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 >

what I get is 2 listview Items,without any changes any help please in how can I correct my code thanks for help

Update:I have used foreach,like this,but I still get a problem with 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));
                            }

                    }

per example when I select the button from the item with index =0 the item with index =1 will be modified,I don't know why I get this result!! >_<


回答1:


After reading it 2 times I understand what you wanna do. Your code logic is wrong. You cant use x:names in listItems cause the code doesnt know to witch one to talk. You have to use bindings.

Data binding overview

INotifyPropertyChanged.PropertyChanged

Create a Class for the List Items

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 >

And then load you items like this

//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;

To get the Item on Buton click

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
        }

You might need to use a IValueConverter for the values to show in xaml but I think this way it ll work, if not you might have to change the datatypes.



来源:https://stackoverflow.com/questions/35294648/select-each-listviewitem-and-apply-a-modification-in-a-universal-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!