C# 按索引号wpf mvvm在listview itemsource中搜索值

C# 按索引号wpf mvvm在listview itemsource中搜索值,c#,wpf,mvvm,C#,Wpf,Mvvm,我使用的是Wpf MVVM,如果我知道item/row的索引号,那么如何根据特定的索引号搜索listview/itemsource中的值 注意:我可以得到索引号,索引号已经知道了 下面是listview的xaml代码 <ListView Grid.Row="1" ItemContainerStyle="{StaticResource FileItemStyle}" ItemsSource="{Binding BarCod

我使用的是Wpf MVVM,如果我知道item/row的索引号,那么如何根据特定的索引号搜索listview/itemsource中的值

注意:我可以得到索引号,索引号已经知道了

下面是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}"
下面是命令,我想把我的逻辑放在这里

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;
        }
    }

下面是我问题的答案,特别感谢@vasily.sib

private ICommand mSearchValueByIndexNumberCommand;
public ICommand SearchValueByIndexNumberCommand
{
    get
    {
        if (mSearchValueByIndexNumberCommand == null)
        {
            mSearchValueByIndexNumberCommand = new DelegateCommand(delegate ()
            {
               int BarCodeId = BarCode[SelectedIndex].BarCodeEntry_ID;

            });
        }
        return mSearchValueByIndexNumberCommand;
    }
}

下面是我问题的答案,特别感谢@vasily.sib

private ICommand mSearchValueByIndexNumberCommand;
public ICommand SearchValueByIndexNumberCommand
{
    get
    {
        if (mSearchValueByIndexNumberCommand == null)
        {
            mSearchValueByIndexNumberCommand = new DelegateCommand(delegate ()
            {
               int BarCodeId = BarCode[SelectedIndex].BarCodeEntry_ID;

            });
        }
        return mSearchValueByIndexNumberCommand;
    }
}

与在XAML中一样,您可以将
ListView.ItemsSource
绑定到ViewModel的
BarCode
,也可以绑定
ListView.SelectedItem
ListView.SelectedIndex
SelectedBarcode
SelectedIndex
,现在,当您在ListView中选择某个项目时,它(ListView)将更新ViewModel中的
SelectBarCode
SelectedIndex
的值


因此,您可以使用
SelectBarCode
BarCode[SelectedIndex]
访问当前选择,就像在XAML中将
ListView.ItemsSource
绑定到ViewModel的
BarCode
一样,但也要绑定
列表视图。SelectedItem
列表视图。SelectedIndex
SelectedBarcode
SelectedIndex
,现在,当您在列表视图中选择某个项目时,它(列表视图)将更新ViewModel中的
SelectedBarcode
SelectedIndex
的值


因此,您可以使用
SelectBarCode
BarCode[SelectedIndex]

访问您当前的选择,它已经在
SelectBarCode
@vasily.sib.谢谢您的回复,但我不想涉及SelectBarCode,然后您可以尝试
BarCode[SelectedIndex]
。但这是一样的。@vasily.sib:将其作为答案发布,并获得一些分数;-)。。。同时
ElementAt(Int32)
也可以使用。请注意,您通常不会同时绑定SelectedIndex和SelectedItem。绑定其中一个或另一个。它已在
SelectBarCode
@vasily.sib中。谢谢您的回复,但我不想涉及SelectBarCode,然后您可以尝试
BarCode[SelectedIndex]
。但这是一样的。@vasily.sib:将其作为答案发布,并获得一些分数;-)。。。同时
ElementAt(Int32)
也可以使用。请注意,您通常不会同时绑定SelectedIndex和SelectedItem。捆绑一个或另一个。
private ICommand mSearchValueByIndexNumberCommand;
public ICommand SearchValueByIndexNumberCommand
{
    get
    {
        if (mSearchValueByIndexNumberCommand == null)
        {
            mSearchValueByIndexNumberCommand = new DelegateCommand(delegate ()
            {
               int BarCodeId = BarCode[SelectedIndex].BarCodeEntry_ID;

            });
        }
        return mSearchValueByIndexNumberCommand;
    }
}