1

我有以下 xaml

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Persons, UpdateSourceTrigger=PropertyChanged}"
                      CanUserSortColumns="True" CanUserReorderColumns="False"
                      SelectionMode="Single" SelectionUnit="FullRow"
                      SelectedItem="{Binding Person, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding EditText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="50"/>
                    </DataTemplate>
                </DataGrid.RowDetailsTemplate>
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Name" Width="*" SortMemberPath="Name">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}" Height="20"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

数据上下文中的代码(绑定到代码隐藏)

public MainWindow()
        {
            this.InitializeComponent();
            this.Persons = new ObservableCollection<Person>
                {
                    new Person
                        {
                            Name = "Alvin"
                        },
                    new Person
                        {
                            Name = "Elvis"
                        },
                };
        }

        private string editText;
        public string EditText
        {
            get { return this.editText; }
            set
            {
                this.editText = value;
                OnPropertyChanged("EditText");
            }
        }

        private ObservableCollection<Person> persons;
        public ObservableCollection<Person> Persons
        {
            get { return this.persons; }
            set
            {
                this.persons = value;
                OnPropertyChanged("Persons");
            }
        }

        private Person person;
        public Person Person
        {
            get { return this.person; }
            set
            {
                this.person = value;
                OnPropertyChanged("Person");
                this.EditText = string.Format("The name of the person is {0}.", this.Person.Name);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

不幸的是,EditText 没有显示在 RowDetailsTemplate 的 TextBlock 中。我不知道为什么。有任何想法吗?

解决方案是

<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid},
                        Path=DataContext.EditText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="50"/>
4

1 回答 1

2

It does not share the same DataContext as your grid.

   <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.EditText}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="50"/>
于 2013-10-31T14:01:00.967 回答