我一直在努力解决这个问题,同时我在互联网上搜索了高低我似乎无法找到适合我的代码制作方式的答案。我已经看到像>here<和>here<这样的页面的结果,但这些都不起作用。
我的代码与数据绑定应用程序的示例代码非常相似,如下所示:
<ListBox ItemsSource="{Binding Items}" Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding LineOne}"/>
<TextBlock Text="{Binding LineTwo}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
除了一个小的区别,那就是我在运行时而不是设计时绑定 ItemsSource,即:
MyListBox.ItemsSource = listCollection;
在这种情况下,listCollection 是一个对象的 ObservableCollection,如下所示:
public class MyObject()
{
public string LineOne { get; set; }
public string LineTwo { get; set; }
}
出于某种原因,我无法让列表突出显示列表中的选定项目。更改为不起作用,因为 Visual Studio 显然不允许 ItemTemplate 和 Resources 在我的代码中使用。
有什么帮助吗?
问候,乔文。