0

i want to set listbox location. In Winform i did this by using this code listbox.Location but in WPF there is no listbox.Location property.

Edit 1:

 var rect = txtBox.GetRectFromCharacterIndex(txtBox.CaretIndex);

 var point = rect.BottomRight;

 lstBox1.Visibility = Visibility.Visible;

 //Want to achieved this
 //TextBox.Location = point;

I am creating something like Intellisense with listbox

chriga
  • 798
  • 2
  • 12
  • 29
Uzair Ali
  • 510
  • 14
  • 32

3 Answers3

0

You should probably read up on WPF layouts, however to you can use the ListBox.Margin to position the ListBox in a hardcoded location.

listbox.Margin = new Thickness( 25, 200, 0, 0 );

or in XAML

<ListBox Margin="25,200,0,0"/>
deloreyk
  • 1,248
  • 15
  • 24
  • i want to set point values on y axis on listbox. In WinForm i did like this `listbox.Location = p` Where p is `p.Y += (int)rtbInput.Font.GetHeight()*2;` – Uzair Ali Jan 30 '14 at 18:03
  • 1
    WPF does not work in that way. It would prefer you to create a structured layout. When you create a layout like this, window resizing and other such features are handled pretty much for you.[Consider this simple tutorial](http://wpftutorial.net/LayoutProperties.html). – deloreyk Jan 30 '14 at 19:17
0

A ListBox's location is determined relative to the control it is contained in. To set a location within it's parent control you can use the HorizontalAlignment, VerticalAlignment and Margin Properties.

Here is an example:

<Window x:Class="WpfApplication14.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
         <ListBox HorizontalAlignment="Left" Height="100" Margin="197,105,0,0" VerticalAlignment="Top" Width="100"/>
     </Grid>
</Window>

Note - all these properties are available programmatically as well.

Thanks,

Eric

Eric Scherrer
  • 3,328
  • 1
  • 19
  • 34
  • I used This `var rect = txtBox.GetRectFromCharacterIndex(txtBox.CaretIndex); var point = rect.BottomRight; lstBox1.Margin = new Thickness(0, point.Y, 0, 0);` On textbox GotFocus Event but the problem is when textbox is focused listbox just docked to the main windows – Uzair Ali Jan 30 '14 at 18:50
0

It all depends on what the parent panel is to determine how to set the position of your listbox. You'll need to read more about Layouts in WPF. Let's look at 2 panels to get you started, Grid and Canvas.

<Grid>
  <ListBox x:Name="lb" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>

lb.Margin = new Thickness(10,10,0,0);

The example above sets the ListBox lb in the Grid at position (10,10).

<Canvas>
  <ListBox x:Name="lb"/>
</Canvas>

Canvas.SetTop(lb, 10);
Canvas.SetLeft(lb, 10);

The example above does the same for lb in a Canvas.

As you can see, it depends on what type of panel that you put your listbox into to be able to set the position correctly.

evanb
  • 3,061
  • 20
  • 32