我有一个GridView
模板设置如下:
<GridView.ItemTemplate>
<DataTemplate x:DataType="models:SampleModel">
<Grid Height="112" Padding="12" Width="227">
<TextBlock HorizontalAlignment="Center"
Text="{x:Bind Description}"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
SampleModel
类只有两个属性,Id: int
和Description: string
。 GridView现在绑定到SampleModel
type列表。像这样:
var list = new List<SampleModel>
{
new SampleModel { Id = 1, Description = "Apple" },
new SampleModel { Id = 2, Description = "Orange" },
new SampleModel { Id = 3, Description = "Banana" }
};
MyGridView.ItemsSource = list;
这是我的问题。在我们的程序中实现的另一个方法(返回int
值的集合)仅抛出Id
。如果这个方法返回,例如,new int[] { 2, 3 }
,我如何获得绑定到Orange和Banana的GridViewItem
对象?这是我到目前为止所得到的,无济于事:
var source = MyGridView.DataContext as List<SampleModel>;
foreach (var id in new int[] { 2, 3 })
{
var fruit = source.FirstOrDefault(a => a.Id == id);
GridViewItem gvi = MyGridView.ContainerFromItem(fruit) as GridViewItem;
gvi.IsSelected = true;
}
通常,如果您知道使用GridView的绑定来显示数据,那么您还应该知道绑定可以为您完成大部分工作。在您的情况下,毫无疑问,绑定也可以帮助您实现目标。
您只需要声明一个额外的属性作为每个项目的背景。然后,您可以使用linq语句从集合中查找特定项并更改其属性值。
我根据您上面的代码片段为您制作了一个代码示例。
<GridView x:Name="MyGridView" ItemsSource="{x:Bind list}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:SampleModel">
<Grid Height="112" Padding="12" Width="227" Background="{x:Bind ItemBackground,Mode=OneWay}">
<TextBlock HorizontalAlignment="Center"
Text="{x:Bind Description}"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
public class SampleModel:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
}
}
private int _Id;
public int Id
{
get => _Id;
set
{
if (_Id != value)
{
_Id = value;
RaisePropertyChanged("Id");
}
}
}
private string _Description;
public string Description
{
get =>_Description;
set
{
if (_Description != value)
{
_Description = value;
RaisePropertyChanged("Description");
}
}
}
private SolidColorBrush _ItemBackground = new SolidColorBrush(Colors.Transparent);
public SolidColorBrush ItemBackground
{
get => _ItemBackground;
set
{
if (_ItemBackground !=value)
{
_ItemBackground = value;
RaisePropertyChanged("ItemBackground");
}
}
}
}
private ObservableCollection<SampleModel> list { get; set; }
public MainPage()
{
this.InitializeComponent();
list = new ObservableCollection<SampleModel>
{
new SampleModel { Id = 1, Description = "Apple" },
new SampleModel { Id = 2, Description = "Orange" },
new SampleModel { Id = 3, Description = "Banana" }
};
}
var tem_list = list.Where(x => x.Id == 1|| x.Id == 2).ToList();
foreach (var item in tem_list)
{
item.ItemBackground = new SolidColorBrush(Colors.LightYellow);
}
为什么不通过Items
属性进行枚举,该属性包含您添加到GridView
的所有项目?
喜欢 :
for (int i = 0; i < MyGridView.Items.Count; i++)
{
if(MyGridView.Items[i] is SampleModel item && (item.ID == 2 || item.ID == 3))
{
var gridViewItem = MyGridView.ContainerFromItem(item);
/// do something with this GridViewItem
}
}