WPF 将组合框选择的项目绑定到列表视图当前项目
WPF Bind combobox selected item to listview current item
我是 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 集合并在应绑定到集合项目的每个项目上添加一个组合框。
<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
但是,如果我 select 另一个学生在组合框中,模型上的集合值不会改变:
MessageBox.Show(ClassroomStudents[0].Name); //Always return John
我找到了一些建议绑定到 属性(即 ID)的答案,但我想绑定到集合项(学生)。
谢谢,
罗多.
我能想到的最简单的解决方法是将您的 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}"
>
我希望这足以给你一个起点。您的代码中有一些与您的问题没有直接关系的附带问题,但可能需要注意才能使整个过程正常运行。
我是 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 集合并在应绑定到集合项目的每个项目上添加一个组合框。
<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
但是,如果我 select 另一个学生在组合框中,模型上的集合值不会改变:
MessageBox.Show(ClassroomStudents[0].Name); //Always return John
我找到了一些建议绑定到 属性(即 ID)的答案,但我想绑定到集合项(学生)。
谢谢, 罗多.
我能想到的最简单的解决方法是将您的 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}"
>
我希望这足以给你一个起点。您的代码中有一些与您的问题没有直接关系的附带问题,但可能需要注意才能使整个过程正常运行。