3

I'm using adorner to display a watermark inside my textbox. but when i set FlowDirection of window to RightToLeft, text inside adorner (which is textblock) is inverse!!!

Is that a bug or i should change something?

enter image description here

and here is full code of 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
    }

}

and i use it:

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

thanks in advance :)

Jalal
  • 6,594
  • 9
  • 63
  • 100
  • Are you saying that it is inverting it twice, or you specifically don't want the watermark to be inverse? Cause to me it looks correct... You posted some text in your code, then it set the direction RightToLeft (it inverted it). – myermian Mar 06 '12 at 21:20
  • text should not to be inversed! FlowDirection just inverse controls alignment in window, nothing more! i don't want the watermark be inverse – Jalal Mar 06 '12 at 21:26
  • Well, looking at the Adorner.cs source code there is a comment about "Bug 1383424". I can't understand if that's a known bug or a fix to that bug... If it is a bug, try explicitly setting the FlowDirection on the `TextBlock` to `LeftToRight` and see if that works? – myermian Mar 06 '12 at 21:57
  • previously try that! not work – Jalal Mar 06 '12 at 22:16

3 Answers3

2

Well, finally i found another solution to solve this. i used RenderTransofrm to mirror control inside adorner like this:

    <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" put pivot in center of control and ScaleX="-1" flip it horizontally.

Jalal
  • 6,594
  • 9
  • 63
  • 100
0

Actually, this isn't a bug ... it's just implicit...

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

To cancel this implicit behaviour, you need to explicitly set the FlowDirection of each inner element that wouldn't match your requirements ;)

Axel Samyn
  • 160
  • 1
  • 12
0

I think I had seen some bug issues with Adorner on the MSDN while I was doing some research on adorner (which I never really used because they didnt suit my needs.).

I cant find the link to where the issues of adorners were discussed, but Im pretty sure its out there.

squelos
  • 1,189
  • 6
  • 16
  • mmm, if it's a bug, so i should use Image instate of TextBlock. It's bad idea but only one – Jalal Mar 06 '12 at 21:27
  • Im not 100% sure about this exact bug, but im pretty sure about 1year ago I read some stuff abouf issues with Adorner, and I believe there was some issue about RightToLeft. So this might be it. – squelos Mar 06 '12 at 23:10
  • i submit it as a bug here: http://connect.microsoft.com/VisualStudio/feedback/details/729341/textblock-inside-adorner-display-inverse-text-when-flowdirection-is-righttoleft#details – Jalal Apr 03 '12 at 18:48