选择每个ListViewItem并在通用应用程序中应用修改

时间:2016-02-09 14:32:43

标签: c# win-universal-app windows-10-universal

我有一个显示这些信息的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个listview项目,没有任何变化 请帮助我如何更正我的代码 谢谢你的帮助

更新:我使用了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));
                            }

                    }

每个例子当我从index = 0的项目中选择按钮时,index = 1的项目将被修改,我不知道为什么我得到这个结果!! &GT; _&LT;

1 个答案:

答案 0 :(得分:2)

阅读2次后,我明白你想做什么。你的代码逻辑错了。你不能在listItems中使用x:names导致代码不知道如何谈话。 你必须使用绑定。

Data binding overview

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中,但我认为这样可以工作,否则您可能需要更改数据类型。