绑定到wpf中父元素的属性

时间:2010-03-13 18:59:20

标签: wpf properties element bind parent

'我想将RichTextBox的Height属性绑定到GridView的Row的Height属性。我怎样才能做到这一点?我不知道如何获得Row的高度,因为我无法访问xaml中的Row。我想做什么。

Ancestor类型应该是GridViewHeaderRow,但我不知道它的级别...

修改

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

...

1 个答案:

答案 0 :(得分:21)

我不知道你的RadGridView。但我要尝试的第一件事就是使用RelativeSource绑定FindAncestor来走向可视树,直到找到GridViewHeaderRow并绑定到它的Height属性。

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

您可能需要走到树上才能找到RadGridView,然后再向下走到标题行。

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

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

取决于RadGridView的实现。