WPF中的BUG?当FlowDirection为RightToLeft时,Adorner中的TextBlock显示反向文本

时间:2012-03-06 21:06:04

标签: c# wpf watermark adorner right-to-left

我正在使用装饰器在我的文本框中显示水印。但是当我将窗口的FlowDirection设置为RightToLeft时,adorner(文本块)中的文本是反向的!

这是一个错误还是我应该改变什么?

enter image description here

以下是adorner的完整代码:

namespace Hezareh.Modules.Accounting  
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Media;


    internal class WatermarkAdorner : Adorner
    {
        #region Private Fields

        private readonly ContentPresenter contentPresenter;

        #endregion

        #region Constructor

        public WatermarkAdorner(UIElement adornedElement, object watermark) :
            base(adornedElement)
        {
            this.IsHitTestVisible = false;
            this.contentPresenter = new ContentPresenter();
            this.contentPresenter.Content = watermark;
            this.contentPresenter.Opacity = 0.5;
            this.contentPresenter.Margin = new Thickness(Control.Margin.Left + Control.Padding.Left, Control.Margin.Top + Control.Padding.Top, 0, 0);
            if (this.Control is ItemsControl && !(this.Control is ComboBox))
            {
                this.contentPresenter.VerticalAlignment = VerticalAlignment.Center;
                this.contentPresenter.HorizontalAlignment = HorizontalAlignment.Center;
            }

            // Hide the control adorner when the adorned element is hidden
            Binding binding = new Binding("IsVisible");
            binding.Source = adornedElement;
            binding.Converter = new BooleanToVisibilityConverter();
            this.SetBinding(VisibilityProperty, binding);
        }

        #endregion

        #region Protected Properties

        protected override int VisualChildrenCount
        {
            get { return 1; }
        }

        #endregion

        #region Private Properties

        private Control Control
        {
            get { return (Control)this.AdornedElement; }
        }

        #endregion

        #region Protected Overrides

        protected override Visual GetVisualChild(int index)
        {
            return this.contentPresenter;
        }

        protected override Size MeasureOverride(Size constraint)
        {
            // Here's the secret to getting the adorner to cover the whole control
            this.contentPresenter.Measure(Control.RenderSize);
            return Control.RenderSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            this.contentPresenter.Arrange(new Rect(finalSize));
            return finalSize;
        }

        #endregion
    }

}

我用它:

        <toolkit:AutoCompleteBox Margin="5" Text="" Name="searchCategoriesTextBox">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <i:InvokeCommandAction Command="{Binding SearchCommand}"  />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <local:WatermarkService.Watermark>
                <TextBlock TextAlignment="Left" Text="جستجو" FontFamily="Tahoma" Margin="3, -3, 3, 0" />
            </local:WatermarkService.Watermark>
        </toolkit:AutoCompleteBox>
提前谢谢 :)

3 个答案:

答案 0 :(得分:2)

好吧,最后我找到了解决这个问题的另一个解决方案。我使用RenderTransofrm在装饰器内镜像控制:

    <local:WatermarkService.Watermark>
        <TextBlock Name="watermarkTextBox" 
                    Text="{x:Static resources:Resources.SearchCodingTreeView}" 
                    RenderTransformOrigin="0.5,0.5" TextAlignment="Right">
        <TextBlock.RenderTransform>
            <ScaleTransform ScaleX="-1" />
        </TextBlock.RenderTransform>
        </TextBlock>
    </local:WatermarkService.Watermark>

RenderTransformOrigin="0.5,0.5"将枢轴置于控制中心,ScaleX="-1"将其水平翻转。

答案 1 :(得分:0)

我想我在MSDN上看到了Adorner的一些bug问题,而我正在对adorner进行一些研究(我从未真正使用它,因为它们不适合我的需求。)。

我无法找到讨论装饰问题的链接,但我很确定它在那里。

答案 2 :(得分:0)

实际上,这不是错误,只是隐含的...

来源:https://wpf.2000things.com/2013/02/21/761-how-flowdirection-affects-horizontalcontentalignment/

要取消此隐式行为,您需要显式设置每个不符合您要求的内部元素的FlowDirection;)