Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过ListView SelectedIndex获取ObservableCollection项_C#_Wpf_Mvvm - Fatal编程技术网

C# 如何通过ListView SelectedIndex获取ObservableCollection项

C# 如何通过ListView SelectedIndex获取ObservableCollection项,c#,wpf,mvvm,C#,Wpf,Mvvm,我创建了一个ListView并将ObservableCollection设置为ItemsSource 然后,我可以使用一个int来绑定SelectedIndex,这很好 但我不知道在我选择时如何获取项目的详细信息 我想获取ObservableCollection,然后将其绑定到文本块上显示 我的TextBlock和ListView是不同的用户控件。主窗口中的ViewModel 所以我想知道如何通过ListView SelectedIndex获取ObservableCollection项 还是其他

我创建了一个ListView并将ObservableCollection设置为ItemsSource

然后,我可以使用一个int来绑定SelectedIndex,这很好

但我不知道在我选择时如何获取项目的详细信息

我想获取ObservableCollection,然后将其绑定到文本块上显示

我的TextBlock和ListView是不同的用户控件。主窗口中的ViewModel

所以我想知道如何通过ListView SelectedIndex获取ObservableCollection项

还是其他方法来解决呢

Thx

MainWinodw中的ViewModel:

public class TestVM : INotifyPropertyChanged
{
    private int _index;
    public int Index
    {
        get
        {
            return _index;
        }
        set
        {
            _index = value;
            RaisePropertyChanged("Index");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(String propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private ObservableCollection<User> _items;
    public ObservableCollection<User> Items
    {
        get
        {
            return _items;
        }
        private set
        {
            _items = value;
            RaisePropertyChanged("Items");
        }
    }

    ObservableCollection<User> _collection;
    public ObservableCollection<User> Collection
    {
        get
        {
            return _collection;
        }
        private set
        {
            _collection = value;
            RaisePropertyChanged("Collection");
        }
    }

    ListCollectionView _groupView;
    public ListCollectionView GroupView
    {
        get
        {
            return _groupView;
        }
        private set
        {
            _groupView = value;
            RaisePropertyChanged("GroupView");
        }
    }

    public TestVM()
    {
        Collection = new ObservableCollection<User>();
        Collection.Add(new User() { Name = "John Doe1", Age = 10, group = "Group 1" });
        Collection.Add(new User() { Name = "Jane Doe2", Age = 20, group = "Group 1" });

        Collection.Add(new User() { Name = "Sammy Doe", Age = 30, group = "Group 2" });
        Collection.Add(new User() { Name = "Sammy Doe1", Age = 40, group = "Group 2" });
        Collection.Add(new User() { Name = "Sammy Doe2", Age = 50, group = "Group 2" });

        Collection.Add(new User() { Name = "Sammy Doe3", Age = 60, group = "Group 3" });
        Collection.Add(new User() { Name = "Sammy Doe4", Age = 70, group = "Group 3" });

        GroupView = new ListCollectionView(Collection);
        GroupView.GroupDescriptions.Add(new PropertyGroupDescription("group"));
    }
}

public class User
{
    public string Name { set; get; }
    public int Age { set; get; }

    public string group { get; set; }
}
公共类TestVM:INotifyPropertyChanged
{
私有整数指数;
公共整数索引
{
得到
{
回归指数;
}
设置
{
_指数=价值;
RaisePropertyChanged(“索引”);
}
}
公共事件属性更改事件处理程序属性更改;
私有void RaisePropertyChanged(字符串propertyName)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
私人可观测收集项目;
公共可观测收集项目
{
得到
{
退货(物品);;
}
专用设备
{
_项目=价值;
增加财产变更(“项目”);
}
}
可观察收集(observedcollection);;
公开收集
{
得到
{
退货(收款);;
}
专用设备
{
_收集=价值;
RaiseProperty变更(“收款”);
}
}
ListCollectionView\u组视图;
公共列表集合视图组视图
{
得到
{
返回组视图;
}
专用设备
{
_组视图=值;
RaisePropertyChanged(“GroupView”);
}
}
公共TestVM()
{
集合=新的ObservableCollection();
添加(新用户(){Name=“John Doe1”,Age=10,group=“group 1”});
添加(新用户(){Name=“Jane Doe2”,年龄=20,group=“group 1”});
添加(新用户(){Name=“Sammy Doe”,Age=30,group=“group 2”});
添加(新用户(){Name=“Sammy Doe1”,年龄=40,group=“group 2”});
添加(新用户(){Name=“Sammy Doe2”,Age=50,group=“group 2”});
添加(新用户(){Name=“Sammy Doe3”,Age=60,group=“group 3”});
添加(新用户(){Name=“Sammy Doe4”,年龄=70,group=“group 3”});
GroupView=新建ListCollectionView(集合);
GroupView.groupdescription.Add(新属性groupdescription(“组”);
}
}
公共类用户
{
公共字符串名称{set;get;}
公共整数{set;get;}
公共字符串组{get;set;}
}
UserControl1中的ListView:

<ListView Margin="10" Name="lv" ItemsSource="{Binding GroupView}" SelectedIndex="{Binding Index}">
        <ListView.View>
            <GridView>
                <local:GridViewColumnExt Header="Name" Width="120" DisplayMemberBinding="{Binding Name}"/>
                <local:GridViewColumnExt x:Name="colAge" Header="Age" Width="50">
                    <local:GridViewColumnExt.CellTemplate>
                        <DataTemplate>
                            <Button Content="{Binding Age}"></Button>
                        </DataTemplate>
                    </local:GridViewColumnExt.CellTemplate>
                </local:GridViewColumnExt>
            </GridView>
        </ListView.View>
        <ListView.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>

UserControl2中的TextBlock(只想显示项目详细信息):


MainWindow.xmal

<Grid>
        <Grid.DataContext>
            <local:TestVM/>
        </Grid.DataContext>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <local:StepList Grid.Column="0"></local:StepList>
        <local:ItemDetail Grid.Column="1"></local:ItemDetail>
    </Grid>

如果希望ListView中所选项目的详细信息显示在文本框中,则必须在文本框中设置绑定

e、 g

编辑3:好吧,忘了我说的话。尝试:

添加到您的Listview

SelectedItem="{Bindig SelectedUser , UpdateSourceTrigger="PropertyChanged"}"
向ViewModel添加带有propertychange通知的property SelectedUser

public User SelectedUser{
     get { return _selectedUser; }
     set
     {
        if (value == _selectedUser) return;
        _selectedUser= value;
        RaisePropertyChanged("SelectedUser");
     }
  }
添加到您的文本框:

 Text="{Binding SelectedUser.PropertyWhichShouldShow, UpdateSourceTrigger="PropertyChanged"}"

事实上,我的TextBlock和ListView是不同的UserControl。主窗口中的ViewModel。如何获取SelectedItem。在此处输入属性以显示,并在此处输入列表视图的名称@黃柏森 请提供相关课程的代码。我的回答与你提供的信息一样精确。好的,我提供了一些内容,我不知道你需要什么。所以,如果你想知道什么,就告诉我。Thx@黃柏森 请添加主窗口xaml。至少在加载用户控件的位置。我尝试使用Text=“{Binding SelectedIndex,ElementName=lv,UpdateSourceTrigger=“PropertyChanged”}”,但它不起作用。我想它不知道“lv”。请看我更新的答案。
public User SelectedUser{
     get { return _selectedUser; }
     set
     {
        if (value == _selectedUser) return;
        _selectedUser= value;
        RaisePropertyChanged("SelectedUser");
     }
  }
 Text="{Binding SelectedUser.PropertyWhichShouldShow, UpdateSourceTrigger="PropertyChanged"}"