Devexpress Chart控件 Y轴标题垂直显示

  通常情况下 用下边的 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;
                }
            }
        }

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值