Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind to a property of a parent element in wpf

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

...

like image 457
msfanboy Avatar asked Mar 13 '10 18:03

msfanboy


People also ask

What is two way binding WPF?

In two way we can change data in sources control and target control. Target to source and source to target means if we change source control value then it will automatically change target control value. And vice-a versa for source control. That's why it's called two-way data binding.

What is path binding WPF?

Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.

What is one way binding in WPF?

In One Way binding, source control updates the target control, which means if you change the value of the source control, it will update the value of the target control. So, in our example, if we change the value of the slider control, it will update the textbox value, as shown below.


1 Answers

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.

like image 178
Travis Heseman Avatar answered Sep 25 '22 02:09

Travis Heseman