Reputation: 619
I have 2 ListviewItems,each one has a btnStar button,one of those Buttons works only,per example when I click on btnStar of ListViewItem1 it modifies btnStar of ListViewItem2's Background,I can't modify the Background color of btnStar of ListViewItem1 when I click on it
this is my xaml:
<ListView x:Name="listme">
<ListView.ItemTemplate >
<DataTemplate >
<Grid>
...
<Button Background="{Binding ButtonColor}" x:Name="btnStar"
Click="btnStar_Click"/>
</Grid>
</DataTemplate >
</ListView.ItemTemplate >
</ListView >
and this is the method that I have use when I click on the btnStar Button :
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;
}
this is how I get the 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));
}
}
}
How can I check if the Button of the selected ListViewItem is clicked,should I use foreach in this method ??
thanks for help
Upvotes: 0
Views: 52
Reputation: 8025
Something like this:
Change:
<Button Background="{Binding ButtonColor}" x:Name="btnStar" Click="btnStar_Click"/>
To:
<Button Background="{Binding ButtonColor}" x:Name="btnStar" Tag="{Binding}" Click="btnStar_Click"/>
Change:
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;
}
To:
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;
}
Change all these line:
listItem.ButtonColor= new SolidColorBrush(Colors.Yellow);
listItem.ButtonColor= new SolidColorBrush(Colors.Gray);//Gray
To:
listItem.ButtonColor= Brushes.Yellow;
listItem.ButtonColor= Brushes.Gray;//Gray
and change
private SolidColorBrush buttonColor;
public SolidColorBrush ButtonColor
{
get { return buttonColor; }
set
{
buttonColor = value;
NotifyPropertyChanged("ButtonColor");
}
}
to this:
private Brush buttonColor;
public Brush ButtonColor
{
get { return buttonColor; }
set
{
buttonColor = value;
NotifyPropertyChanged("ButtonColor");
}
}
Upvotes: 1