我有一组类,它包含的项目列表和标题:选择多个列表项目
public class MyGroup {
public MyGroup(string _header){
header = _header;
}
protected string header = "";
public string Header
{
get { return header; }
}
protected List<MyGroupItem> item = new List<MyGroupItem>();
public List<MyGroupItem> Item
{
get { return item; }
}
}
public class MyGroupItem {
public MyGroupItem(string _name, double _multiplier){
name = _name;
multiplier = _multiplier;
}
protected double multiplier = 1.0;
protected string name = "";
public string Name
{
get { return name; }
}
}
到目前为止好。在我的主类,我组的观察的集合,我填充它是这样的:
protected ObservableCollection<MyGroup> groups = new ObservableCollection<MyGroup>();
public ObservableCollection<MyGroup> Groups
{
get { return groups; }
}
protected MyGroupItem currentItem;
public MyGroupItem CurrentItem
{
get { return currentItem; }
set
{
if (currentItem== value) return;
currentItem= value;
NotifyPropertyChanged("CurrentItem");
}
}
....
var GroupA = new MyGroup("Group A");
GroupA.MyGroupItem.Add("Item 1", 1.0);
Groups.Add(GroupA);
currentItem = GroupA.MyGroupItem[0];
所有上述的简单显示了如何已经设置我的课和观察的名单。现在,我切换到xaml。
<ItemsControl ItemsSource="{Binding Path=Groups}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="Transparent" ClipToBounds="True" Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:MyGroup}">
<StackPanel>
<TextBlock Text="{Binding Path=Header}"></TextBlock>
<ListView ItemsSource="{Binding Path=MyGroupItem}" SelectedItem="{Binding Path=DataContext.CurrentItem, ElementName=ControlRoot}">
<ListView.ItemTemplate>
<DataTemplate DataType="local:MyGroupItem">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
所以,基本上我有一个ItemControl显示多个项目列表。控件应该为组的名称放置标题,然后显示该组中特定项目的列表视图。布局完美...但是,问题来了,当我处理选定项目。基本上,列表视图允许我在多个组中选择一个项目......这意味着我可能在任何给定时间选择多个项目。例如,假设我选择了组A中的第一个项目,但是,我将选择更改为组B中的第二个项目。因为组B是一个单独的列表,它允许我激活该项目......但它不会取消选择组A中的项目。我想要的是这个多列表组作为单个列表。这可能吗?我是否需要单独设置SelectionChanged事件?如果是的话,我该如何确定选择何时被更改,以便清除所有列表中的选定项目,并且只显示用户刚选择的正确项目?
这工作完美!感谢彻底的回应! – andyopayne