Answered Control's left and control's Top in WPF

  • 2012年7月11日 19:07
     
     

    Hello everyone,

    I have a requirement where i need to show a small wpf borderless window right below a text box ( just like a drop down list open up ).In short, i am trying to set the placement target for the new window to show up at the bottom of the textbox.

    Any suggestions ? Moreover TransformToAncestor method didn't help me much.

    Thanks,

    BR,

    JollyWagoner

すべての返信

  • 2012年7月12日 0:51
     
     回答済み コードあり

    Try using a popup and align it to the parent control with PlacementTarget:

    	<TextBox Name="myTextBox" Text="My Textbox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    	<Popup IsOpen="True" PlacementTarget="{Binding ElementName=myTextBox}" >
    		<ContentControl>
    			<Border Background="White" >
    				<Label>The Popup</Label>
    			</Border>
    		</ContentControl>
    	</Popup>





  • 2012年7月25日 14:55
     
     

    Hello everyone,

    I forgot to mention but its strictly a window and not popup control that i want to use, otherwise i would have already used it.The popup is limited in the sense that "stays above everything in the world" as long as it remains open.Which is potentially a drawback as everything ( i.e window , error dialog ) that opens up is behind the popup all the time.

    Thanks,

    BR,

    JollyWagoner

  • 2012年7月25日 23:04
     
     回答済み コードあり

    Hello Jolly.

    You could try something like the following...

    private void myTextBlock_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
    	Point p1 = myTextBlock.TranslatePoint(new Point(0,0), this);
    	Point p2 = myTextBlock.PointFromScreen(p1);
    	double a = myTextBlock.Height + 10; //You may want to play around with the + 10 margin.
    	Window1 win = new Window1();
    	win.Top = (p2.Y * -1) + a;
    	win.Left = p2.X * -1;
    	win.Show();			
    }

    ~Christine

    P.S. Sorry my sample used a TextBlock instead of TextBox, but same concept.


    My Gallery


  • 2012年7月26日 1:44
     
     回答済み
    Hi

    One little improval:
    Use "a = myTextBlock.ActualHeight + 10";
    If you choose "Height" and there is no explicit value for height
    on the element, "a" becomes "double.NaN" and also win.Top.
    In consequence there is no valid placement for the window
    and the os will position it somewhere arbitrarily on the screen.

    Chris
  • 2012年7月30日 4:15
    モデレータ
     
     

    Hi JollyWagoner,

    I think your issue has been resolved, so I will mark your thread as "Answered", if you need further help, please let me know.

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.