当组合框本身是数据网格列的一部分时,无法将组合框选定项绑定到TextBox

时间:2015-06-03 11:33:56

标签: c# wpf datagrid combobox

我希望在我在DataGrid中使用的comboxbox中更改所选索引时,在文本框中绑定主题名称

这是我的代码   ////

   <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));
        }
    }
   }
 }

请在选择更改后提供任何解决方案,将组合框的项目绑定到texbox。

1 个答案:

答案 0 :(得分:0)

您可以通过在DataGrid上设置 IsSynchronizedWithCurrentItem =&#34; True&#34; 来实现此目的

 <DataGrid x:Name="GridWithComboBox" IsSynchronizedWithCurrentItem="True" .....

并需要将您的ComboBox绑定更改为...

SelectedValue="{Binding Subject,UpdateSourceTrigger=PropertyChanged}"