绑定到WPF中父元素的属性 [英] Bind to a property of a parent element in wpf
问题描述
'我想将RichTextBox的Height属性绑定到GridView的Row的Height属性.我怎样才能做到这一点?我不知道如何获取行的高度,因为我无法通过xaml访问行.
'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.
祖先类型应该是GridViewHeaderRow,但是我不知道它的级别...
The Ancestor type should be GridViewHeaderRow , but I do not know its level...
<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>
...
推荐答案
在这里我不了解您的RadGridView.但是我首先要尝试的是使用 RelativeSource 与FindAncestor绑定沿着视觉树前进,直到找到GridViewHeaderRow并绑定到其Height属性.
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 }}}" ...
您可能必须走到树上才能找到RadGridView,然后再向下走到标题行.
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 }}}" ...
或
... Height="{Binding Rows[0].Height,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type RadGridView }}}" ...
取决于RadGridView的实现.
Depends on the implementation of RadGridView.
这篇关于绑定到WPF中父元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!