RowDetailsTemplate binding

此生再无相见时 提交于 2019-12-25 11:58:02

问题


I have the following 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>

The code in the datacontext (binded to the code-behind)

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

Unfortunately the EditText is not displayed in the TextBlock of the RowDetailsTemplate. I don't know why. Any ideas?

The Solution is

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

回答1:


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


来源:https://stackoverflow.com/questions/19708548/rowdetailstemplate-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!