Reputation: 818
I'm using Wpf MVVM, if i know the index number of item/row then how can i search the value in listview/itemsource by specific index number.
Note: i can get index number, index number will already be known.
below is the xaml code for listview
<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}"
and ObservableCollection for itemsource
private ObservableCollection<BarCodeModel> mBarCode = null;
public ObservableCollection<BarCodeModel> BarCode
{
get
{
mBarCode = mBarCode ?? new ObservableCollection<BarCodeModel>();
return mBarCode;
}
}
and below code is for model
public class BarCodeModel
{
public int BarCodeEntry_ID { get; set; }
public string BarCodeEntry_Title { get; set; }
and below is the command where i want to put my logic
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;
}
}
Upvotes: 0
Views: 947
Reputation: 4002
As in your XAML you bind your ListView.ItemsSource
to BarCode
of your ViewModel, but also bind ListView.SelectedItem
and ListView.SelectedIndex
to SelectBarCode
and SelectedIndex
, now, when you select some Item in a ListView, it (ListView) will update values of SelectBarCode
and SelectedIndex
in your ViewModel.
So, you can access your current selection with SelectBarCode
or BarCode[SelectedIndex]
.
Upvotes: 2
Reputation: 818
Below is answer to my question, a special thanks to @vasily.sib
private ICommand mSearchValueByIndexNumberCommand;
public ICommand SearchValueByIndexNumberCommand
{
get
{
if (mSearchValueByIndexNumberCommand == null)
{
mSearchValueByIndexNumberCommand = new DelegateCommand(delegate ()
{
int BarCodeId = BarCode[SelectedIndex].BarCodeEntry_ID;
});
}
return mSearchValueByIndexNumberCommand;
}
}
Upvotes: 0