1

I have a Grid similar to this:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="0" Text="MyHeader1"/>
    <myNamespace:MyRotatedTextBlock
        Grid.Row="1" Grid.Column="0" MyText="MyHeader2"/>
</Grid>

and myNamespace:MyRotatedTextBlock is a custom WPF control like this:

<TextBlock Text="{Binding MyText}"
    HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock.LayoutTransform>
         <RotateTransform Angle="90"/>
    </TextBlock.LayoutTransform>
</TextBlock>

The problem is when I open the window, I can't see the second row which contains the rotated text. But if I replace the Height of the second row (which is set to "Auto") with "100" then I can see that the second row is shown and it contains MyHeader2

Bizhan
  • 16,157
  • 9
  • 63
  • 101
  • 1
    Does your control contain any text? The TextBlock should size to its content automatically. I can see that you've bound the Text property of your control to 'MyText' and that you're setting that property to 'MyHeader2' - is it possible that you've got a problem with your Text binding in your custom control? – Kai G Jan 23 '12 at 02:19
  • (I edited the question.) No it is bound correctly as it's shown when I manually extend the row's height. – Bizhan Jan 23 '12 at 09:13
  • Why do you actually introduce a new property MyText and not use the Text property of the TextBlock where you (probably) derive from? – Youp Bernoulli Jan 23 '12 at 09:22
  • How silly of me! Thank you. That fixed the problem:) Please post your answer so that I can accept it. – Bizhan Jan 23 '12 at 09:36

2 Answers2

1

You can also derive from TextBlock (instead of userControl) like this:

<TextBlock x:Class="WpfGridRowHeightStackOverflowQuestion.MyRotatedTextBlock"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             HorizontalAlignment="Center"
             VerticalAlignment="Center">
    <TextBlock.LayoutTransform>
         <RotateTransform Angle="90"/>
    </TextBlock.LayoutTransform>
</TextBlock>

Then just use the Text property from the TextBlock like this:

<myNamespace:MyRotatedTextBlock Grid.Row="1" Grid.Column="0" Text="MyHeader2"></myNamespace:MyRotatedTextBlock>

EDIT

This way it works as a UserControl as well (because the elementname of the binding is specified explicitly to the user control's name):

<UserControl x:Class="WpfGridRowHeightStackOverflowQuestion.MyRotatedTextBlock"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
                         Name="CustomRotatedTextBlock">
    <TextBlock Text="{Binding ElementName=CustomRotatedTextBlock,Path=MyText}" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock.LayoutTransform>
         <RotateTransform Angle="90"/>
    </TextBlock.LayoutTransform>
    </TextBlock>
</UserControl>

Then i code behind use Change Notification by INotifyPropertyChanged (which WPF relies heavy upon ;)

public partial class MyRotatedTextBlock : UserControl, INotifyPropertyChanged
{
    public MyRotatedTextBlock()
    {
        InitializeComponent();
    }

    private String _myText;
    public String MyText
    {
        get { return _myText; }
        set { 
            _myText = value;

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("MyText"));
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • I did reproduce your project and (for this moment) couldn't get it to work with a UserControl. But I will still have a look at it... – Youp Bernoulli Jan 23 '12 at 09:38
  • It has something to do with the binding according to my observations. When you simply use the inherited Text property (instead of Binding to MyText) in the xaml of the usercontrol it works fine. – Youp Bernoulli Jan 23 '12 at 09:43
  • `MyText` Property was a simple `DependencyProperty` created by Visual Studio Snippet `propdp`. I couldn't find anything relevant in it to configure. – Bizhan Jan 23 '12 at 09:58
  • and by the way I think replacing `Name="CustomRotatedTextBlock"` and `Text="{Binding ElementName=CustomRotatedTextBlock,Path=MyText}"` with `DataContext={Binding RelativeSource={RelativeSource Self}}"` and `Text="{Binding Path=MyText}"` is better. – Bizhan Jan 23 '12 at 13:26
0

Did you try UpdateLayout ? try UpdateLayout for the grid after opening the window

simo
  • 23,342
  • 38
  • 121
  • 218