C# 当combobox本身是datagrid列的一部分时,无法将combobox选定项绑定到文本框
当我在DataGrid中使用的comboxbox中更改所选索引时,我希望将主题名称绑定到文本框中 这是我的密码 //// ///C# 当combobox本身是datagrid列的一部分时,无法将combobox选定项绑定到文本框,c#,wpf,datagrid,combobox,C#,Wpf,Datagrid,Combobox,当我在DataGrid中使用的comboxbox中更改所选索引时,我希望将主题名称绑定到文本框中 这是我的密码 //// /// 更改选择时,请提供将组合框的项目绑定到文本框的任何解决方案。您可以通过在DataGrid上设置IsSynchronizedWithCurrentItem=“True”来实现此目的 <DataGrid x:Name="GridWithComboBox" IsSynchronizedWithCurrentItem="True" ..... IsSynchroni
更改选择时,请提供将组合框的项目绑定到文本框的任何解决方案。您可以通过在DataGrid上设置IsSynchronizedWithCurrentItem=“True”来实现此目的
<DataGrid x:Name="GridWithComboBox" IsSynchronizedWithCurrentItem="True" .....
IsSynchronizedWithCurrentItem=“True”无法使用当前代码。我是否也需要更改文本框绑定?在文本框绑定中使用SelectedItem而不是SelectedValue,但我得到的输出与以前相同。当我在组合框中更改主题名称时,它不会更新我的文本框值。当您更改组合选择时,它是否会影响其他单元格?在StudentModel上实现INotifyPropertyChanged接口,绑定仅在触发PropertyChanged事件时才起作用
using System;
using System.Windows;
using System.Windows.Data;
namespace WpfGridDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
StudentDetails studentDetailsList = new StudentDetails();
this.DataContext = studentDetailsList;
}
}
}
using System.Collections.ObjectModel;
namespace WpfGridDemo
{
public class StudentDetails
{
public ObservableCollection<Subjects> SubjectList
{
get;
set;
}
public ObservableCollection<StudentModel> StudentDetailsList
{
get;
set;
}
public StudentDetails()
{
StudentDetailsList = new ObservableCollection<StudentModel>();
StudentDetailsList.Add(new StudentModel() { Name = "Rohit", Subject = "Java" });
StudentDetailsList.Add(new StudentModel() { Name = "Tarun", Subject = "C#" });
SubjectList = new ObservableCollection<Subjects>();
SubjectList.Add(new Subjects() { SubjectNames = "Java" });
SubjectList.Add(new Subjects() { SubjectNames = "C#" });
SubjectList.Add(new Subjects() { SubjectNames = "Python" });
SubjectList.Add(new Subjects() { SubjectNames = "Rails" });
}
}
}
using System.ComponentModel;
namespace WpfGridDemo
{
public class StudentModel : INotifyPropertyChanged
{
private string subject;
public string Subject
{
get
{
return this.subject;
}
set
{
this.subject = value;
this.OnPropertyChanged("Subject");
}
}
public string Name
{ get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Subjects : INotifyPropertyChanged
{
private string _subjectNames;
public string SubjectNames
{
get
{
return this._subjectNames;
}
set
{
this._subjectNames = value;
this.OnPropertyChanged("SubjectNames");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
<DataGrid x:Name="GridWithComboBox" IsSynchronizedWithCurrentItem="True" .....
SelectedValue="{Binding Subject,UpdateSourceTrigger=PropertyChanged}"