【问题标题】:Unable to Bind a combobox selected item to a TextBox when combobox itself is a part of a datagrid column当组合框本身是数据网格列的一部分时,无法将组合框选定项绑定到文本框
【发布时间】:2015-06-03 11:33:55
【问题描述】:

当我在 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));
        }
    }
   }
 }

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

【问题讨论】:

    标签: c# wpf datagrid combobox


    【解决方案1】:

    您可以通过在 DataGrid 上设置 IsSynchronizedWithCurrentItem="True" 来实现此目的

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

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

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

    【讨论】:

    • IsSynchronizedWithCurrentItem="True" 不适用于当前代码。我还需要更改文本框绑定吗?
    • 在 TextBox 的绑定中使用 SelectedItem 而不是 SelectedValue
    • 我仍然得到与以前相同的输出。当我在组合框中更改主题名称时,它不会更新我的文本框值。
    • 当你改变组合选择时会影响另一个单元格吗?
    • 在 StudentModel 上实现 INotifyPropertyChanged 接口,绑定仅在 PropertyChanged 事件触发时起作用
    猜你喜欢
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    相关资源
    最近更新 更多