您也可以像这样从 TextBlock(而不是 userControl)派生:
<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 工作(因为绑定的元素名称明确指定给用户控件的名称):
<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
}