问题描述
我避免问这个问题,但是无法设置ListBox的选定索引.我已经阅读了其他线程并应用了设置,但这是行不通的.
I avoided to ask this question, but the ListBox's selected index can no be set. I have read the other threads and applied the settings, but it doesn't work.
<ListBox ItemsSource="{Binding}"
HorizontalAlignment="Right"
Name="lstReading" Height="Auto"
SelectedIndex="{Binding BookmarkSelectedIndex}">
在something.xaml.cs中,我进行设置
In the something.xaml.cs, I am settings
lstReading.DataContext = IQText;
其中,IQText是一个 IEnumerable< dictIQ>
,并且包含 BookmarkSelectedIndex
作为数据元素.可以使用IQText中的其他数据元素,但不能设置listindex.有人可以让我知道为什么吗?
Where, IQText is an IEnumerable<dictIQ>
and includes the BookmarkSelectedIndex
as data element. Other data elements from IQText can be used but the listindex can't be set. Could someone please let me know why?
推荐答案
dictIQ
类里面有 BookmarkSelectedIndex
吗?因此,每个项目而不是每个集合都有一个 BookmarkSelectedIndex
!
Are you have BookmarkSelectedIndex
inside of dictIQ
class? So, you have one BookmarkSelectedIndex
per item, not per collection!
您可以在 dictIQ
之外创建单独的属性 BookmarkSelectedIndex
或创建从 ObservalbeCollection< dictIQ>
继承并具有其他属性的类BookmarkSelectedIndex
:
You can create separate property BookmarkSelectedIndex
outside of dictIQ
or create class that inherited from ObservalbeCollection<dictIQ>
and have additional property BookmarkSelectedIndex
:
public class CollectionWithIndex: ObservalbeCollection<dictIQ>
{
public int BookmarkSelectedIndex { get; set; }
}
希望您选择最适合您的解决方案
I hope you choose best solution suitable for you
这篇关于不能设置数据绑定ListBox的SelectedListIndex属性,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!