我创建了一个ListView并将ObservableCollection设置为ItemsSource。
然后,我可以使用int来绑定SelectedIndex,它运行良好。
但是,当我选择时,我不知道如何获取项目的详细信息。
我想获取ObservableCollection,然后将其绑定到TextBlock上显示。
我的TextBlock和ListView是不同的UserControl。 MainWindow中的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; }
}
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(只想显示项目详细信息):
<WrapPanel>
<TextBlock Text="SelectdIndex: "/>
<TextBlock Text="{Binding Index}" />
</WrapPanel>
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>
答案 0 :(得分:1)
如果您希望ListView中所选项目的详细信息显示在文本框中,则必须在文本框中设置绑定。
e.g。
Text="{Binding SelectedItem.EnterThePropertyToShowhere, ElementName=EnterTheNameOfyourListViewhere, UpdateSourceTrigger="PropertyChanged"}"
编辑3:好吧忘了我说的话。尝试:
添加到Listview
SelectedItem="{Bindig SelectedUser , UpdateSourceTrigger="PropertyChanged"}"
使用propertychange Notification
向ViewModel添加属性SelectedUserpublic User SelectedUser{
get { return _selectedUser; }
set
{
if (value == _selectedUser) return;
_selectedUser= value;
RaisePropertyChanged("SelectedUser");
}
}
添加到文本框中:
Text="{Binding SelectedUser.PropertyWhichShouldShow, UpdateSourceTrigger="PropertyChanged"}"