gpt4 book ai didi

xamarin - Xamarin窗体无法更改Listview SelectedItem上的图像

转载 作者:行者123 更新时间:2023-12-03 11:02:12 25 4
gpt4 key购买 nike

我在listview中有一个标签和图像。我必须在列表 View 上选择一个项目时更改图像。在viewmodel中选择项目时,我正在获取图像名称和标签文本的当前值,但无法将图像更改为新图像。我已经更改了图像后调用了onproperty。但是值未更新。请帮助..

<StackLayout >
<ListView x:Name="ser_area_list" HasUnevenRows="True" ItemsSource="{Binding Servicearealist }" SelectedItem="{Binding dd}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal" >
<Image Source="{Binding ImageName}" WidthRequest="30" HeightRequest="30" />
<Label Text="{Binding area}" VerticalTextAlignment="Center" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>

View 模型:
 public class PopChkVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public List<HSA_value> servicearealist = new List<HSA_value>();

HSA_value ab = new HSA_value();
public HSA_value dd
{
get
{
return ab;
}
set
{
ab = value;

if (ab == null)
return;

OnPropertyChanged("dd");
Debug.WriteLine(ab);
ab.ImageName = "checkedbox.png";
OnPopertyChanged("ImageName");

}
}

string imagename;
public string ImageName
{
set
{
imagename = value;

OnPopertyChanged("ImageName");

}
get
{
return imagename;
}
}

void OnPopertyChanged(string ImageName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(ImageName));
}

public List<HSA_value> Servicearealist
{
get { return servicearealist; }
set
{
servicearealist = value;
OnPropertyChanged("Servicearealist");
}
}
public PopChkVM()
{
GetHomeServiceAreas();
}

protected virtual void OnPropertyChanged(string servicearealist)
{
if (PropertyChanged == null)
return;

PropertyChanged(this, new PropertyChangedEventArgs(servicearealist));
}

private async void GetHomeServiceAreas()
{

RestClient dataService = new RestClient();
var items = await dataService.GetServiceArea();
if (items.status.Equals("success"))
{
Servicearealist = new List<HSA_value>();
foreach (HSA_Data data in items.data)
{
HSA_value hSA_Value = new HSA_value();
hSA_Value.area = data.area;
hSA_Value.ImageName = "unchecked.png";
Servicearealist.Add(hSA_Value);
}
}
else
{
DependencyService.Get<IToast>().LongAlert("No Data Available!");
}
}

}

模型:
public class HSA_value
{
public string id { get; set; }
public string area { get; set; }
public string ImageName { get; set; }
}

最佳答案

在HSA_value类中执行以下代码

public class HSA_value : INotifyPropertyChanged
{
public string id { get; set; }
public string area { get; set; }
string imageName = string.Empty;
public string ImageName {
get
{
return imageName;
}
set
{
imageName = value;
PropertyChanged(this, new PropertyChangedEventArgs(ImageName));
}
}
}

关于xamarin - Xamarin窗体无法更改Listview SelectedItem上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52835855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com