【问题标题】:search value in listview itemsource by index number wpf mvvm通过索引号 wpf mvvm 在 listview itemsource 中搜索值
【发布时间】: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&lt;T&gt;(Int32) 也可用。 msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx
  • 请注意,您通常不会同时绑定 SelectedIndex 和 SelectedItem。绑定一个或另一个。

标签: c# wpf mvvm


【解决方案1】:

就像在 XAML 中一样,您现在将 ListView.ItemsSource 绑定到 ViewModel 的 BarCode,但也将 ListView.SelectedItemListView.SelectedIndex 绑定到 SelectBarCodeSelectedIndex,现在,当您在 ListView 中选择一些项目时, 它 (ListView) 将更新您的 ViewModel 中 SelectBarCodeSelectedIndex 的值。

因此,您可以使用SelectBarCodeBarCode[SelectedIndex] 访问您当前的选择。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-06
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      相关资源
      最近更新 更多