[英]Refreshing ListView in Xamarin.Forms for UWP
我的系统的详细信息是操作系统:Windows 10 Pro N Visual Studio Enterprise 2015 Xamarin.Forms 2.3.1..114
我创建了一个选项卡式视图,在其中使用Navigation.PushModalAsync方法导航到新页面。 在视图中,我有一个带有自定义数据模板的列表视图。 数据模板属于ViewCell,其中包含两张图片和一个标签。
我想做的是,无论何时选择一个单元格,我都会显示选中行的图像,而当选择另一行时,则隐藏其他行图像并显示当前选择的图像。
首次加载视图时,我将第一行设置为选中状态,并且一切正常,但是当我选择任何其他行时,ListView不会刷新。 Image IsVisible属性设置正确,但是没有反映在列表上。
请参阅以下代码以供参考
ListView的代码
var listView = new ListView();
listView.ItemsSource = StaticData.ListData;
listView.ItemTemplate = new DataTemplate(typeof(CustomDataCell));
listView.VerticalOptions = LayoutOptions.FillAndExpand;
listView.BackgroundColor = Color.White;
listView.SeparatorVisibility = SeparatorVisibility.Default;
listView.RowHeight = 30;
listView.SeparatorColor = Color.White;
listView.ItemTapped += (sender, e) =>
{
if (e == null) return;
selectedValue = (e.Item as ValiditySchema).Value;
SelectValidityItem(listView,selectedValue); // In this method I am setting the IsSelected property to true and other rows IsSelected property to false.
};
CustomDataCell的代码
public class CustomDataCell : ViewCell
{
public Label CellText { get; set; }
public BoxView ImageDetail { get; set; }
public Image CheckedImage { get; set; }
public CustomDataCell()
{
CellText = new Label();
CellText.FontAttributes = FontAttributes.Bold;
CellText.SetBinding(Label.TextProperty, "Text");
CellText.VerticalOptions = LayoutOptions.Center;
CellText.HorizontalOptions = LayoutOptions.Start;
CellText.TextColor = Color.Black;
ImageDetail = new BoxView();
ImageDetail.WidthRequest = 20;
ImageDetail.HeightRequest = 10;
ImageDetail.SetBinding(BoxView.BackgroundColorProperty, "ColorName");
//declaring image to show the row is selected
CheckedImage = new Image();
CheckedImage.Source = "Images/checked.png";
CheckedImage.HorizontalOptions = LayoutOptions.CenterAndExpand;
CheckedImage.VerticalOptions = LayoutOptions.Center;
CheckedImage.SetBinding(Image.IsVisibleProperty, "IsSelected");
var ContentCell = new StackLayout();
ContentCell.Children.Add(ImageDetail);
ContentCell.Children.Add(CellText);
ContentCell.Children.Add(CheckedImage);
ContentCell.Spacing = 5;
ContentCell.Orientation = StackOrientation.Horizontal;
var maiCell = new StackLayout();
maiCell.Orientation = StackOrientation.Vertical;
maiCell.Children.Add(ContentCell);
View = maiCell;
}
}
为了让ListView知道ItemsSource中的项目已更改,您需要对该特定项目引发INotifyPropertyChanged事件。
通常,不是将数据直接绑定到ListView,而是希望按照MVVM模式为每个项目使用ViewModel表示形式:
查看<-> ViewModel <->模型
因此,您需要做的是为StaticData.ListData
的项目创建一个ViewModel:
public class ListItemViewModel : INotifyPropertyChanged
{
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set {
_isSelected = value;
OnPropertyChanged();
}
}
// more properties here...
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
然后,您可以将IsSelected
属性绑定到图像的Visibility属性。
这样,当您在ViewModel中更改IsSelected
时,将触发正确的事件,并且ListView现在知道某些内容已更改,并且需要刷新视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.