Bind to a property of a parent element in wpf

msfanboy picture msfanboy · Mar 13, 2010 · Viewed 45.1k times · Source

'I want to bind the Height property of the RichTextBox to the Height Property of the GridView`s Row. How can I do that? I do not know how to get the Row's Height as I can not access the Row in xaml what I would like to do.

The Ancestor type should be GridViewHeaderRow , but I do not know its level...

EDIT:

 <my:RadGridView  Height="524" RowHeight="300" ItemsSource="{Binding Lessons}" AutoGenerateColumns="False" Name="dataGrid1" VerticalAlignment="Top" SelectionMode="Single" CanUserSortColumns="False" IsFilteringAllowed="False">

            <my:RadGridView.Columns>
                <my:GridViewDataColumn  DataMemberBinding="{Binding SchoolclassName}" Header="Schoolclass" Width="0.1*" />
                <my:GridViewDataColumn DataMemberBinding="{Binding SubjectName}"     Header="Subject"      Width="0.1*" />

                <my:GridViewDataColumn  Width="0.3*" Header="Homework">
                    <my:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <RichTextBox Height="{Binding ElementName=dataGrid1,Path=RowHeight}" >
                                <FlowDocument>
                                    <Paragraph>
                                        <Run Text="{Binding Homework}"/>
                                    </Paragraph>
                                </FlowDocument>
                            </RichTextBox>                                
                        </DataTemplate>
                    </my:GridViewDataColumn.CellTemplate>


<my:RadGridView Height="524" ItemsSource="{Binding Lessons}" AutoGenerateColumns="False" Name="dataGrid1" VerticalAlignment="Top" SelectionMode="Single" CanUserSortColumns="False" IsFilteringAllowed="False">
            <my:RadGridView.Columns>

                <my:GridViewDataColumn Name="ContentColumn" Width="0.3*" Header="Content">
                    <my:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <RichTextBox Height="{Binding ElementName=MyRowNameToBindTo,Path=Height}">
                                <FlowDocument>
                                    <Paragraph>
                                        <Run Text="{Binding Content}"/>
                                    </Paragraph>
                                </FlowDocument>
                            </RichTextBox>
                        </DataTemplate>
                    </my:GridViewDataColumn.CellTemplate>

...

Answer

Travis Heseman picture Travis Heseman · Mar 13, 2010

I don't know about your RadGridView here. But the first thing I'd try is using a RelativeSource Binding with FindAncestor to walk up the visual tree until a GridViewHeaderRow is found and bind to its Height property.

 ... Height="{Binding Height, 
              RelativeSource={RelativeSource Mode=FindAncestor, 
                                 AncestorType={x:Type GridViewHeaderRow }}}" ...

You may have to walk up the tree to find the RadGridView and then walk back down it to the header row.

 ... Height="{Binding HeaderRow.Height, 
              RelativeSource={RelativeSource Mode=FindAncestor, 
                                 AncestorType={x:Type RadGridView }}}" ...

or

 ... Height="{Binding Rows[0].Height, 
              RelativeSource={RelativeSource Mode=FindAncestor, 
                                 AncestorType={x:Type RadGridView }}}" ...

Depends on the implementation of RadGridView.