5

有没有办法将 Callout 控件的 PointAnchor 设置为Microsoft.Expression.ControlsWPF Control ,

我试过类似的东西

public static void PointMe(this Callout me,UIElement ui)
{
         Me.AnchorPoint = ????? // don't know how to get the ui coordinates
}

在此处输入图像描述

4

2 回答 2

1

有几种方法可以实现这一点。一种是将所有内容放在 a 中Canvas,然后Callout根据您想要将标注放置在旁边的控件的 x/y 画布坐标移动 。

一种更灵活的方法是使用TransformToAncestor方法来定位要将标注锚定到的控件相对于某个祖先元素的位置。在这种情况下,定位的最佳元素是根元素。

这是一个工作示例:

XAML

<Grid x:Name="LayoutRoot">
    <ed:Callout x:Name="MyCallout"
                Width="200"
                Height="100"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                AnchorPoint="0,1.5"
                CalloutStyle="Rectangle"
                Content="Callout"
                Stroke="Black" />

    <Button x:Name="LeftButton"
            Width="75"
            Margin="100,0,0,150"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            Click="OnLeftButtonClick"
            Content="Left" />

    <Button x:Name="RightButton"
            Width="75"
            Margin="300,0,0,150"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            Click="OnRightButtonClick"
            Content="Right" />

</Grid>

代码背后

private void OnLeftButtonClick( object sender, RoutedEventArgs e )
{
    MoveCallout( LeftButton );
    e.Handled = true;
}


private void OnRightButtonClick( object sender, RoutedEventArgs e )
{
    MoveCallout( RightButton );
    e.Handled = true;
}

private void MoveCallout( FrameworkElement element )
{
    // find the position of the element to anchor against relative to the root object
    Point point = element.TransformToAncestor( LayoutRoot ).Transform( new Point( 0, 0 ) );

    // take into account the width of the anchor element
    double x = point.X + element.ActualWidth;

    // take into account the height of the anchor element and the callout
    // add a little wiggle room as well
    double y = point.Y - element.ActualHeight - MyCallout.ActualHeight - 10;

    MyCallout.Content = element.Name;

    // Move the callout to the new coordinates
    MyCallout.RenderTransform = new TranslateTransform( x, y );
}
于 2012-10-05T15:29:32.567 回答
1

注意:根据 OP 的重新解释,即所需的结果是移动 AnchorPoint 而不是整个标注,我正在添加另一个答案,以便我的第一个答案可以在需要时留给其他人使用,因为它移动了标注。


似乎没有任何内置框架方法可以相对于屏幕上的另一个元素移动 Callout 的 AnchorPoint。

标注的锚点基于标注的上角和左角的相对位置。您可以通过计算标注与任何其他元素之间的距离来移动 AnchorPoint,然后进行一些数学运算以得出相对定位。以下应该有效:

XAML

<Window x:Class="so.ExpCallout.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
        Title="Callout Anchor Point"
        Width="625"
        Height="350"
        WindowStartupLocation="CenterScreen">

    <Grid x:Name="LayoutRoot">
        <ed:Callout x:Name="MyCallout"
                    Width="200"
                    Height="100"
                    Margin="200,50,0,0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    AnchorPoint="0,1.5"
                    CalloutStyle="Rectangle"
                    Content="Callout"
                    Stroke="Black" />

        <Button x:Name="LeftButton"
                Width="100"
                Height="25"
                Margin="50,200,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Click="OnLeftButtonClick"
                Content="Left" />

        <Button x:Name="RightButton"
                Width="100"
                Height="25"
                Margin="450,200,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Click="OnRightButtonClick"
                Content="Right" />

    </Grid>

</Window>

代码背后

using System.Windows;
using System.Windows.Media;

namespace so.ExpCallout
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnRightButtonClick( object sender, RoutedEventArgs e )
        {
            AdjustCalloutAnchor( RightButton );
            e.Handled = true;
        }

        private void OnLeftButtonClick( object sender, RoutedEventArgs e )
        {
            AdjustCalloutAnchor( LeftButton );
            e.Handled = true;
        }

        private void AdjustCalloutAnchor( FrameworkElement el )
        {
            // locate the positions of the callout and the element to point to
            Point calloutPoint = MyCallout.TransformToAncestor( LayoutRoot ).Transform( new Point( 0, 0 ) );
            Point elementPoint = el.TransformToAncestor( LayoutRoot ).Transform( new Point( 0, 0 ) );

            double vertOffset = calloutPoint.Y + MyCallout.ActualHeight;
            double horizOffset = elementPoint.X;
            if( calloutPoint.X > elementPoint.X )
            {
                // increase the horizontal offset if the callout 
                // is to the right of the element
                horizOffset += el.ActualWidth;
            }

            double vertDistance = ( elementPoint.Y + el.ActualHeight ) - vertOffset;
            double horizDistance = calloutPoint.X - horizOffset;

            // add some cushion between the element and the new AnchorPoint
            int pad = 5;

            // the AnchorPoint is a relative distance based on the actual height
            // and width of the callout.
            double x = ( horizDistance / ( MyCallout.ActualWidth + pad ) ) * -1;
            double y = ( vertDistance / ( MyCallout.ActualHeight + pad ) ) + 1;

            // set the new AnchorPoint location
            MyCallout.AnchorPoint = new Point( x, y );

            MyCallout.Content = string.Format( "I'm pointing at {0}", el.Name );
        }
    }
}
于 2012-10-09T23:51:10.873 回答