通常情况下 用下边的 VerticalTextBlock 就可以直接显示垂直文本 但是有时候只能把字横过来 文本整体没有横过来 所以转了90
<dxc:XYDiagram2D.SecondaryAxesY>
<dxc:SecondaryAxisY2D Name="AxisY1" GridLinesVisible="True" Alignment="Near" Thickness="3">
<dxc:AxisY2D.Strips>
<dxc:Strip Brush="#FFFFFFFF" BorderThickness="3" BorderColor="Black" />
</dxc:AxisY2D.Strips>
<dxc:AxisY2D.Title>
<dxc:AxisTitle HorizontalAlignment="Left" FontStretch="ExtraCondensed">
<ContentPresenter Name="Titles" Content="数 值" VerticalAlignment="Center"
HorizontalAlignment="Left" Margin="0,-150,0,-150"
RenderTransformOrigin="0.5,0.5">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<A2U:VerticalTextBlock
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=Content}"
Foreground="Black" FontSize="14" FontWeight="ExtraBlack"
FontFamily="Arial,SimSun" />
</DataTemplate>
</ContentPresenter.ContentTemplate>
<ContentPresenter.RenderTransform>
<RotateTransform Angle="90" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
</dxc:AxisTitle>
</dxc:AxisY2D.Title>
<dxc:AxisY2D.Range>
<dxc:AxisRange dxc:AxisY2D.AlwaysShowZeroLevel="False" />
</dxc:AxisY2D.Range>
<dxc:AxisY2D.Label>
<dxc:AxisLabel FontWeight="ExtraBold" Foreground="Black" />
</dxc:AxisY2D.Label>
</dxc:SecondaryAxisY2D>
</dxc:XYDiagram2D.SecondaryAxesY>
public class VerticalTextBlock : Control
{
public VerticalTextBlock()
{
IsTabStop = false;
var templateXaml =
@"<ControlTemplate " +
#if SILVERLIGHT
"xmlns='http://schemas.microsoft.com/client/2007' " +
#else
"xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
#endif
"xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>" +
"<Grid Background=\"{TemplateBinding Background}\">" +
"<TextBlock x:Name=\"TextBlock\" TextAlignment=\"Center\"/>" +
"</Grid>" +
"</ControlTemplate>";
#if SILVERLIGHT
Template = (ControlTemplate)XamlReader.Load(templateXaml);
#else
using(var stream = new MemoryStream(Encoding.UTF8.GetBytes(templateXaml)))
{
Template = (ControlTemplate)XamlReader.Load(stream);
}
#endif
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_textBlock = GetTemplateChild("TextBlock") as TextBlock;
CreateVerticalText(_text);
}
private string _text { get; set; }
private TextBlock _textBlock { get; set; }
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(VerticalTextBlock), new PropertyMetadata(OnTextChanged));
private static void OnTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
((VerticalTextBlock)o).OnTextChanged((string)(e.NewValue));
}
private void OnTextChanged(string newValue)
{
CreateVerticalText(newValue);
}
private void CreateVerticalText(string text)
{
_text = text;
if (null != _textBlock)
{
_textBlock.Text = "";
bool first = true;
foreach (var c in _text)
{
if (!first)
{
_textBlock.Inlines.Add(new LineBreak());
}
_textBlock.Inlines.Add(new Run { Text = c.ToString() });
first = false;
}
}
}
}