Question

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

Was it helpful?

Solution

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
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top