选择每个 ListViewItem 并在通用应用程序中应用修改 [英] select each ListViewItem and apply a modification in a universal App
问题描述
我有一个显示这些信息的 ListView,我想要的是为我的按钮显示不同的背景如果我得到 fav = 1,如果我得到 aime = 1,则显示不同的图像,:JSON 格式:
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",
}
]
}
这是我的代码:
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 个列表视图项,没有任何变化请帮助我如何更正我的代码感谢帮助
what I get is 2 listview Items,without any changes any help please in how can I correct my code thanks for help
更新:我已经使用了 foreach,就像这样,但我仍然遇到 ListViewItems 的问题:
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));
}
}
每个例子当我从索引=0的项目中选择按钮时,索引=1的项目将被修改,我不知道为什么会得到这个结果!!>_<
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!! >_<
推荐答案
读了 2 遍后我明白你想做什么了.你的代码逻辑是错误的.你不能在 listItems 中使用 x:names ,因为代码不知道让一个人说话.您必须使用绑定.
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.
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 >
然后像这样加载你的项目
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;
点击按钮获取物品
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 中,但我认为这种方式可以工作,否则您可能需要更改数据类型.
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.
这篇关于选择每个 ListViewItem 并在通用应用程序中应用修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!