RowDetailsTemplate绑定

时间:2013-10-31 13:58:10

标签: c# wpf datagrid

我有以下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>

datacontext中的代码(绑定到代码隐藏)

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"/>

1 个答案:

答案 0 :(得分:2)

它与您的网格不共享相同的DataContext。

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