【发布时间】:2018-06-27 18:52:23
【问题描述】:
我正在使用 Wpf MVVM,如果我知道项目/行的索引号,那么我如何通过特定的索引号搜索列表视图/项目源中的值。
注意:我可以得到索引号,索引号已经知道了。
下面是listview的xaml代码
<ListView
Grid.Row="1"
ItemContainerStyle="{StaticResource FileItemStyle}"
ItemsSource="{Binding BarCode, IsAsync=True}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectedIndex="{Binding SelectedIndex}"
SelectedItem="{Binding SelectBarCode,
UpdateSourceTrigger=PropertyChanged}"
SelectionMode="Single"
Style="{StaticResource ListItemsMain}"
和用于 itemsource 的 ObservableCollection
private ObservableCollection<BarCodeModel> mBarCode = null;
public ObservableCollection<BarCodeModel> BarCode
{
get
{
mBarCode = mBarCode ?? new ObservableCollection<BarCodeModel>();
return mBarCode;
}
}
以下代码用于模型
public class BarCodeModel
{
public int BarCodeEntry_ID { get; set; }
public string BarCodeEntry_Title { get; set; }
下面是我要放置逻辑的命令
private ICommand mSearchValueByIndexNumberCommand;
public ICommand SearchValueByIndexNumberCommand
{
get
{
if (mSearchValueByIndexNumberCommand == null)
{
mSearchValueByIndexNumberCommand = new DelegateCommand(delegate ()
{
// search BarCodeEntry_ID in BarCode where SelectedIndex is 5 (or other value)
});
}
return mSearchValueByIndexNumberCommand;
}
}
【问题讨论】:
-
已经在
SelectBarCode -
@vasily.sib 感谢您的回复,但我不想涉及 SelectBarCode
-
那你可以试试
BarCode[SelectedIndex]。但这是一回事。 -
@vasily.sib:将其发布为答案并获得分数 ;-) ... 同时...
ElementAt<T>(Int32)也可用。 msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx -
请注意,您通常不会同时绑定 SelectedIndex 和 SelectedItem。绑定一个或另一个。