views:

939

answers:

1

I'm new to XAML, so please be aware my question may contain some topic misundrestanding.

Is it posible to bind XAML usercontrol global (relative to window) position to check if it's currently visible on screen? Usercontrol is inserted inside ScrollViewer and I think on something like:

  <UserControl x:Class="Test.MessageControl"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Width="300">
      <StackPanel >
          <Label Name="LabelTest" 
          Content="{Binding RelativeSource={RelativeSource Self}, 
          Path=MAGIC-GOES-HERE-Location.Y }" />
      </StackPanel>
   </UserControl>
+1  A: 

No, it is not possible to get the "location" of your controls in XAML (not directly like we are used to, at least). In WPF, controls don't have "location" properties anymore. Controls and screen elements only have width and height properties, which won't help you find where they are in the window.

BUT: If you really need to know where something is, you CAN figure it out using properties of the parent control. (Microsoft has made it so that this is the only reliable way to do this anymore.)

Example:
If you have a grid with two rows, and your UserControl is in the second row - you can treat the ActualHeight property of the first row like it is the "Location.Y" property of your UserControl, instead of looking to the control itself to provide you with its location.

This is something that you will have to get used to, since there is no longer any way to get around it.

Consider using the <Grid> element somewhere in your design. It provides information about the location of your controls in a very straight-forward kind of way. It works like a smarter version of a dynamicly-sizing HTML table. You can retrive ActualWidth and ActualHeight properties of any row or column - or even of the entire grid itself if you want.

Giffyguy