C#基于另一个ListView的索引向ListView添加数组

C#基于另一个ListView的索引向ListView添加数组,c#,arrays,winforms,listview,C#,Arrays,Winforms,Listview,我有两个ListView和一个字符串数组: string[] groceries = { "Bread", "Milk", "Cheese", "Fruit" }; 当我在第一个ListView上选择索引时,我希望第二个ListView用数组中的杂货重新填充自己 如果你不明白我的意思,请看这里: 当用户在listView1中选择索引0时 获取数组值(面包、牛奶、奶酪和水果) 将它们添加到listView2 您的伪代码没有问题。。。看来你知道你要做什么。看看这是否有帮助,或者至少引导你

我有两个ListView和一个字符串数组:

    string[] groceries = { "Bread", "Milk", "Cheese", "Fruit" };
当我在第一个ListView上选择索引时,我希望第二个ListView用数组中的杂货重新填充自己

如果你不明白我的意思,请看这里:

  • 当用户在listView1中选择索引0时
  • 获取数组值(面包、牛奶、奶酪和水果)
  • 将它们添加到listView2

您的伪代码没有问题。。。看来你知道你要做什么。看看这是否有帮助,或者至少引导你朝着正确的方向前进

当所选索引在第一个
列表视图中更改时:

  • 查看所选索引是否为
    0
    (假设这是您要测试的索引)
  • 然后清除第二个
    列表视图中的项目
    ,并添加您的杂货项目


好吧,我意识到这并不能回答现在您已经澄清的问题,但是我已经开始基于WPF实现提出响应。我为它感到骄傲,所以我要把它贴出来,也许它会帮助其他人

下面的示例代码将根据另一个列表视图的选定索引更改一个列表视图的内容

<Window x:Class="ListViewArrays.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:listViewArrays="clr-namespace:ListViewArrays"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <listViewArrays:ListViewModel x:Key="testData" />
</Window.Resources>
<Grid >
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ListView Grid.Row="0" ItemsSource="{Binding Source={StaticResource testData}, Path=OriginalList}" 
              SelectedIndex="{Binding Source={StaticResource testData}, Path=OriginalListSelectedIndex}" />
    <ListView Grid.Row="1" ItemsSource="{Binding Source={StaticResource testData}, Path=DependentList}"/>
</Grid>

使用下面定义的ViewModel:

    public class ListViewModel : INotifyPropertyChanged
{
    private readonly IEnumerable<string> _originalList = new[] {"Groceries", "Test"};
    private IEnumerable<string> _dependentList;
    private int _originalListSelectedIndex;

    public IEnumerable<string> OriginalList
    {
        get { return _originalList; }
    }

    public IEnumerable<string> DependentList
    {
        get { return _dependentList; }
        set
        {
            _dependentList = value;
            OnPropertyChanged();
        }
    }

    public int OriginalListSelectedIndex
    {
        get { return _originalListSelectedIndex; }
        set
        {
            _originalListSelectedIndex = value;
            //Logic for changing dependent list goes here
            if (_originalListSelectedIndex == 0)
            {
                DependentList = new[] { "Bread", "Milk", "Cheese", "Fruit" };
            }
            else
            {
                DependentList = new[] { "test 1", "test 2" };
            }
        }
    }

}
公共类ListViewModel:INotifyPropertyChanged
{
private readonly IEnumerable _originalList=new[]{“Groceries”,“Test”};
私有IEnumerable_依赖列表;
private int_originalListSelectedIndex;
公共数字原创作家
{
获取{return\u originalList;}
}
公共IEnumerable从属列表
{
获取{return\u dependentList;}
设置
{
_dependentList=值;
OnPropertyChanged();
}
}
public int OriginalListSelectedIndex
{
获取{return\u originalListSelectedIndex;}
设置
{
_originalListSelectedIndex=值;
//更改依赖列表的逻辑如下所示
如果(_originalListSelectedIndex==0)
{
DependentList=新[]{“面包”、“牛奶”、“奶酪”、“水果”};
}
其他的
{
DependentList=new[]{“测试1”、“测试2”};
}
}
}
}

看起来您正试图基于事件填充listview?WPF还是windows窗体?你问什么还不完全清楚。:)您的问题没有明确说明,请通过编辑您的post@jsmithWindows窗体,因为我不使用XML来定义外观。是的,当listView1上的某个索引被选中时,我想用数组中的项目填充listView2。谢谢!这正是我想要的!:D
    public class ListViewModel : INotifyPropertyChanged
{
    private readonly IEnumerable<string> _originalList = new[] {"Groceries", "Test"};
    private IEnumerable<string> _dependentList;
    private int _originalListSelectedIndex;

    public IEnumerable<string> OriginalList
    {
        get { return _originalList; }
    }

    public IEnumerable<string> DependentList
    {
        get { return _dependentList; }
        set
        {
            _dependentList = value;
            OnPropertyChanged();
        }
    }

    public int OriginalListSelectedIndex
    {
        get { return _originalListSelectedIndex; }
        set
        {
            _originalListSelectedIndex = value;
            //Logic for changing dependent list goes here
            if (_originalListSelectedIndex == 0)
            {
                DependentList = new[] { "Bread", "Milk", "Cheese", "Fruit" };
            }
            else
            {
                DependentList = new[] { "test 1", "test 2" };
            }
        }
    }

}