views:

38

answers:

3

Hi I want to bind a lists position to its own height in XAML. So its lower left corner always will be at 0.0 of the canvas. I'm using elementBinding to get the ActualHeight and a converter to invert the property. But the height sent to the converter is 0. How do I solve this or am I going at this the wrong way?

<Canvas x:Name="DisplaySurface">
    <ListBox x:Name="MenuList" Visibility="Visible"  
             Canvas.Top="{Binding ElementName=MenuList, Path=ActualHeight, 
             Converter={StaticResource LamdaConv}, ConverterParameter='val=>-val'}">

         <ListBoxItem Content="item 1" />
         <ListBoxItem Content="item 2" />
         <ListBoxItem Content="item 3" />
         <ListBoxItem Content="item 4" />
         <ListBoxItem Content="item 5" />
         <ListBoxItem Content="item 6" />
     </ListBox>
</Canvas>

-Qanik

+1  A: 

Sounds to me like you are using the wrong control for the job. A Grid can handle this without all this effort:-

 <Grid>

    <Canvas x:Name="DisplaySurface">
    </Canvas>
    <ListBox HorizontalAlignment="Left" VerticalAlignment="Bottom" ...>
       <!-- items --->
    </ListBox>
</Grid> 

Now the List box always appears in the bottom left corner. Not only that but if the total available height is less than the height of all the content in the list box it will be capped at the available height and show scroll bar. Something your code would otherwise have to jump through hoops working out.

AnthonyWJones
A: 

Thanks for your replies!

My goal is to show the listbox on top/above of another control, so I need the canvas to be able to give the listbox a negative position. Think of it like a dropdown going up.

Qanik
This is not an answer so please delete, either move this text as a comment on a specific answer or add it to your question. SO is not always viewed sequentially, the series of answers do not form a conversation, each is an independent answer to the question.
AnthonyWJones
+1  A: 

Try the binding {Binding ActualHeight, RelativeSource={RelativeSource Self},Converter={StaticResource LamdaConv}, ConverterParameter='val=>-val'}

Stephan
This was what I was looking for.Thanks!
Qanik