在通用应用程序中为每个ListViewItem应用Button的修改

时间:2016-02-26 14:54:54

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

我有2个ListviewItems,每个都有一个btnStar按钮,其中一个按钮只能工作,每个例子当我点击ListViewItem1的btnStar它修改了ListViewItem2的背景的btnStar时,我无法修改ListViewItem1的btnStar的背景颜色当我点击它

这是我的xaml:

 <ListView  x:Name="listme">
 <ListView.ItemTemplate >
   <DataTemplate >
     <Grid>
       ...
      <Button Background="{Binding ButtonColor}"  x:Name="btnStar" 
Click="btnStar_Click"/>
     </Grid>
   </DataTemplate >
 </ListView.ItemTemplate >
</ListView >

这是我点击btnStar按钮时使用的方法:

bool btnclicked = false; 
private void btnStar_Click(object sender, RoutedEventArgs e)
        {

            if (btnclicked)
            {
                btnStar.Background = new SolidColorBrush(Colors.Yellow);              
                btnclicked = false;
            }
            else
            {
                btnStar.Background = new SolidColorBrush(Colors.Gray);//Gray
                btnclicked = true;
            }

这就是我获取ListViewItems的方式:

 ObservableCollection<Item> Locals = new ObservableCollection<Item>();
public async void getListePerSearch()
    {
        try
        {
            UriString2 = "URL";
            var http = new HttpClient();
            http.MaxResponseContentBufferSize = Int32.MaxValue;
            var response = await http.GetStringAsync(UriString2);
            var rootObject1 = JsonConvert.DeserializeObject<NvBarberry.Models.RootObject>(response);

           foreach (var item in rootObject1.locals)
                {
                    Item listItem = new Item();
                    if (item.fav == 1)
                        {
                         listItem.ButtonColor= new SolidColorBrush(Colors.Yellow); //Yellow
                        }

                        else
                        {
                        listItem.ButtonColor= new SolidColorBrush(Colors.Gray);//Gray
                        }
                        Locals.Add(listItem);
                }

                listme.ItemsSource = Locals;
   }

Item.cs:

 public class Item : INotifyPropertyChanged
    {

        private SolidColorBrush buttonColor;
        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));
            }
        }
}

如何点击所选ListViewItem的Button,我应该在这个方法中使用foreach吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

这样的事情:

变化:

  <Button Background="{Binding ButtonColor}"  x:Name="btnStar" Click="btnStar_Click"/>

要:

  <Button Background="{Binding ButtonColor}"  x:Name="btnStar" Tag="{Binding}" Click="btnStar_Click"/>

变化:

private void btnStar_Click(object sender, RoutedEventArgs e)
    {

        if (btnclicked)
        {
            btnStar.Background = new SolidColorBrush(Colors.Yellow);              
            btnclicked = false;
        }
        else
        {
            btnStar.Background = new SolidColorBrush(Colors.Gray);//Gray
            btnclicked = true;
        }

要:

private void btnStar_Click(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;
    var item = btn.Tag as Item;
    item.ButtonColor = item.ButtonColor == Brushes.Gray ? Brushes.Yellow : Brushes.Gray;
}

更改所有这些行:

listItem.ButtonColor= new SolidColorBrush(Colors.Yellow); 
listItem.ButtonColor= new SolidColorBrush(Colors.Gray);//Gray

要:

listItem.ButtonColor= Brushes.Yellow; 
listItem.ButtonColor= Brushes.Gray;//Gray

并更改

private SolidColorBrush buttonColor;
public SolidColorBrush ButtonColor
{
    get { return buttonColor; }
    set
    {
        buttonColor = value;
        NotifyPropertyChanged("ButtonColor");
    }
}

到此:

private Brush buttonColor;
public Brush ButtonColor
{
    get { return buttonColor; }
    set
    {
        buttonColor = value;
        NotifyPropertyChanged("ButtonColor");
    }
}