WPF将组合框选中的项目绑定到listview当前项目

时间:2017-05-29 03:09:50

标签: c# wpf listview combobox bind

我是WPF的新手,所以我必须遗漏一些非常明显的东西。

我有这个简单的设置:

 public partial class MainWindow : Window
{
    public ObservableCollection<Student> Students { get; set; }
    public ObservableCollection<Student> ClassroomStudents { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        Students = new ObservableCollection<Student>();

        Students.Add(new Student { ID = 1, Name = "John" });
        Students.Add(new Student { ID = 2, Name = "Paul" });
        Students.Add(new Student { ID = 3, Name = "Ringo"});
        Students.Add(new Student { ID = 4, Name = "George" });

        ClassroomStudents = new ObservableCollection<Student>();
        ClassroomStudents.Add(Students[0]); //View binds it ok at startup (one way)
        ClassroomStudents.Add(Students[1]); //View binds ut ok at startup (one way)

    }


    public class Student : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int ID { get; set; }
        public string Name { get; set; }
    }

    private void bOK_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(ClassroomStudents[0].Name);

}

我想要实现的是显示ClassroomStudents集合并在每个应该绑定到集合项的项目上添加一个Combobox。

    <Grid>
    <ListView x:Name="SortBy" ItemsSource="{Binding ClassroomStudents}" Grid.Column="0">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="80">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>                             
                            <ComboBox                                    
                                ItemsSource="{Binding Students, RelativeSource={ RelativeSource AncestorType=Window}}" 
                                DisplayMemberPath="Name"                                         
                                SelectedItem="{Binding Path=.,Mode=TwoWay}"                                                                        
                                >
                            </ComboBox>
                            <!--<Label Content="{Binding}"></Label>-->
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>                   
            </GridView>
        </ListView.View>
    </ListView>

    <!--OK/CANCEL BUTTONS-->
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="1" Margin="0,10,0,0">
        <Button x:Name="bOK" IsDefault="True" Margin="10" Width="100" Click="bOK_Click">OK</Button>
        <Button x:Name="bCancel" IsCancel="True" Margin="10" Grid.Column="1" Width="100" Click="bCancel_Click">Cancel</Button>
    </StackPanel>
</Grid>

它以一种方式结合在一起:

image

但是如果我在组合框中选择另一个学生,模型上的集合值不会改变:

MessageBox.Show(ClassroomStudents[0].Name); //Always return John

我找到了一些建议绑定到属性(即ID)的答案,但我想绑定到集合Item(Student)。

谢谢, RODO。

1 个答案:

答案 0 :(得分:0)

我能想到的最简单的解决方法是将ClassroomStudents项目包装在选择容器中:

public class StudentContainer : INotifyPropertyChanged
{
    public Student Item { get; set; } // TODO: INotifyPropertyChanged implementation
}

更改收集和初始化:

public ObservableCollection<StudentContainer> ClassroomStudents { get; set; }
// ...
ClassroomStudents.Add(new StudentContainer { Item = Students[0] });

更改XAML

<ComboBox                                    
    ItemsSource="{Binding Students, RelativeSource={ RelativeSource AncestorType=Window}}"
    DisplayMemberPath="Name"
    SelectedItem="{Binding Path=Item,Mode=TwoWay}"
    >

我希望这足以给你一个起点。您的代码中存在一些与您的问题没有直接关系的副问题,但可能需要一些注意才能使整个工作正常运行。