当组合框本身是数据网格列的一部分时,无法将组合框所选项绑定到文本框
本文关键字:组合 一部 分时 选项 文本 绑定 数据 数据网 网格 | 更新日期: 2023-09-27 17:52:51
我想在文本框中绑定主题名称时,所选的索引在复合框中更改,我在DataGrid内使用
这是我的代码////
<Window x:Class="WpfGridDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Students="clr-namespace:WpfGridDemo"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Height="22" Width="113" Text="{Binding ElementName=ComboSubjects,Path= SelectedItem.Subject}"/>
<DataGrid x:Name="ComboSubjects" IsSynchronizedWithCurrentItem="True" Grid.Row="1" AutoGenerateColumns="False"
ItemsSource="{Binding StudentDetailsList, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name }" Header="Student Name" Width="200" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Subjects" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Subject}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="ComboSubjects" SelectedValuePath="SubjectNames"
SelectedValue="{Binding Subject}"
DisplayMemberPath="SubjectNames"
ItemsSource="{Binding Path=DataContext.SubjectList,Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
/////
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上设置IsSynchronizedWithCurrentItem="True"来实现
<DataGrid x:Name="GridWithComboBox" IsSynchronizedWithCurrentItem="True" .....
并且需要将ComboBox Binding更改为…
SelectedValue="{Binding Subject,UpdateSourceTrigger=PropertyChanged}"