英文:
How to get the itemcontainer in Itemscontrol?
问题
以下是翻译好的内容:
我想获取ListView控件的itemcontainer,但始终得到null。
如果我从SelectionChanged事件中获取该项,代码将正常工作。
我不明白为什么会发生这种情况。
英文:
I want to get ListView control 's itemcontainer , but always get null.
If I get the item from the event SelectionChanged,the code will work correctly.
I can't understand why this happens.
答案1
得分: 0
如果你想使用ItemsControl.ContainerFromItem(Object) 方法,参数需要是你设置的数据,而不是来自 ListView.Items
的项对象。
应该像这样:
public List<Model> list { get;set; }
public MainPage()
{
this.InitializeComponent();
list = new List<Model>();
list.Add(new Model { Name = "A" });
list.Add(new Model { Name = "B" });
list.Add(new Model { Name = "C" });
list.Add(new Model { Name = "D" });
mylist.ItemsSource = list;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ListViewItem item = mylist.ContainerFromItem(list[1]) as ListViewItem;
}
英文:
If you want to use ItemsControl.ContainerFromItem(Object) Method, the parameter needs to be the data that you set. Not an item object from the ListView.Items
.
It should be something like this:
public List<Model> list { get;set; }
public MainPage()
{
this.InitializeComponent();
list = new List<Model>();
list.Add(new Model { Name = "A" });
list.Add(new Model { Name = "B" });
list.Add(new Model { Name = "C" });
list.Add(new Model { Name = "D" });
mylist.ItemsSource = list;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ListViewItem item = mylist.ContainerFromItem(list[1]) as ListViewItem;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论