首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RowDetailsTemplate结合

RowDetailsTemplate结合
EN

Stack Overflow用户
提问于 2013-10-31 13:58:10
回答 1查看 1.8K关注 0票数 1

我有以下的xaml

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
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中。我也不知道原因。有什么想法吗?

解决方案是

代码语言:javascript
复制
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid},
                        Path=DataContext.EditText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="50"/>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-31 14:01:00

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

代码语言:javascript
复制
   <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.EditText}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="50"/>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19708548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档