2

我有一个包含ObservableCollection这样的字典:

Dictionary<string, ObservableCollection<Person>> MyDictionary

现在在我的 xaml 中,我正在创建一个itemscontrol使用字典的键作为扩展器和人的集合的listview东西,如下所示:

<ItemsControl   ItemsSource="{Binding MyDictionary}" VerticalAlignment="Center" HorizontalAlignment="Left">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Expander Name="expander" IsExpanded="True">
                            <Expander.Header>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Margin="5" VerticalAlignment="Stretch" HorizontalAlignment="Left" Text="MyString:"/>
                                    <TextBlock Margin="5" VerticalAlignment="Stretch" HorizontalAlignment="Left" Text="{Binding Key}"/>
                                </StackPanel>
                            </Expander.Header>
                            <Expander.Content>
                                <ListView SelectionMode="Single" ItemsSource="{Binding Value}">

                                    <ListView.View>
                                        <GridView  AllowsColumnReorder="True">

                                            <GridViewColumn>
                                                <GridViewColumn.Header>
                                                    <TextBlock Text="Name"  Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </GridViewColumn.Header>
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock Text="{Binding Name}" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn>
                                                <GridViewColumn.Header>
                                                    <TextBlock Text="Last Name" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                </GridViewColumn.Header>
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock Text="{Binding LastName}" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>
                                        </GridView>
                                    </ListView.View>
                                </ListView>
                            </Expander.Content>
                        </Expander>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

现在,如您所见,它创建了一个扩展器集合,每个扩展器在其内容中都有一个列表视图......

我只想让一个列表视图有一个选定的项目,我该怎么做?

如果我不清楚:我将有 3 个Expanders,每个有 1 个ListView,每个ListView有 4-5 个项目,我希望当用户单击listviewitem所有其他ListViews选定项目时将取消选择。

谢谢 !

4

2 回答 2

0

为什么不订阅每个列表视图的选择事件并在其他视图上调用UnselectAll

于 2013-08-01T14:51:34.120 回答
0

嘿,我有个主意。将所有 listBox 的 SelectedItem 绑定到 ViewModel 的相同 SelectedItemProperty,例如

>xaml 这里我有两个 ListBox,其 SelectedItem 绑定到 VM 的相同属性

<StackPanel Height="500" Width="500">
        <ListBox Height="200" ItemsSource="{Binding StudentList1}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedStudent}"></ListBox>
        <ListBox Height="200" ItemsSource="{Binding StudentList2}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedStudent}"></ListBox>
    </StackPanel>

xml.cs

public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }

视图模型

public class ViewModel:INotifyPropertyChanged
    {
        public ViewModel() 
        {
            StudentList1 = new ObservableCollection<Student>();
            StudentList1.Add(new Student() { Name = "abc", Age = 20 });
            StudentList1.Add(new Student() { Name = "abc", Age = 20 });
            StudentList1.Add(new Student() { Name = "abc", Age = 20 });

            StudentList2 = new ObservableCollection<Student>();
            StudentList2.Add(new Student() { Name = "xyz", Age = 30 });
            StudentList2.Add(new Student() { Name = "xyz", Age = 30 });
            StudentList2.Add(new Student() { Name = "xyz", Age = 30 });
        }
        public ObservableCollection<Student> StudentList1 { get; set; }

        public ObservableCollection<Student> StudentList2 { get; set; }

        Student selectedStudent;

        public Student SelectedStudent
        {
            get { return selectedStudent; }
            set { selectedStudent = value; Notify("SelectedStudent"); }
        }
        public void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));

        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

学生班

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

我希望您有一个想法。当您选择一个 Listbox 项目时,SelectedItem 将基于 References 工作,VM 的 SelectedStudent 属性将得到更新,因此所有其他 ListBox SelectedItem 将被取消选择,因为它们在 itemssource 中没有此引用。MVVM 的力量 :)

>更新

Student selectedStudent1;

        public Student SelectedStudent1
        {
            get { return selectedStudent1; }
            set {
                  selectedStudent=null;
                  selectedStudent1 = value;
                  Notify("SelectedStudent1"); }
        }

Student selectedStudent;

        public Student SelectedStudent
        {
            get { return selectedStudent; }
            set {
                  selectedStudent1=null;
                  selectedStudent = value; 
                  Notify("SelectedStudent"); }
        }
于 2013-08-01T15:01:54.203 回答