1

私はこれに似たグリッドを持っています:

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

myNamespace:MyRotatedTextBlockは、次のようなカスタムWPFコントロールです。

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

問題は、ウィンドウを開いたときに、回転したテキストを含む2番目の行が表示されないことです。Heightしかし、2番目の行(に設定されている"Auto")をに置き換える"100"と、2番目の行が表示され、次の行が含まれていることがわかります。MyHeader2

4

2 に答える 2

1

次のように、(userControlの代わりに)TextBlockから派生することもできます。

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

次に、TextBlockのTextプロパティを次のように使用します。

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

編集

このように、それはUserControlとしても機能します(バインディングのelementnameがユーザーコントロールの名前に明示的に指定されているため):

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

次に、INotifyPropertyChangedによる変更通知の使用の背後にあるコード(WPFはこれに大きく依存しています;)

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
}
于 2012-01-23T09:36:32.827 に答える
0

やってみましたUpdateLayoutか?ウィンドウを開いた後、グリッドのUpdateLayoutを試してください

于 2012-01-23T09:19:01.923 に答える