2016-06-10 67 views
0

我正在处理交互式图表,其中当用户单击数据点时显示包含更多信息的弹出窗口。是弹出的定义:将弹出窗口放置在鼠标光标位置的顶部

<Popup IsOpen="{Binding PopupViewModel.IsOpen}" 
     Placement="Mouse" 
     HorizontalOffset="-150"> 
    <Popup.Resources> 
     <DataTemplate DataType="{x:Type ViewModels:DataPointPopUpContentViewModel}"> 
      <Views:DataPointPopUpContentView/> 
     </DataTemplate> 
    </Popup.Resources> 
    <Border BorderThickness="1" BorderBrush="Black" Background="White"> 
     <ContentPresenter Content="{Binding PopupViewModel}" /> 
    </Border> 
</Popup> 

弹出,使用Placement="Mouse"当在鼠标光标的右下方的默认位置。不过,我希望将弹出窗口放置在鼠标光标的顶部边缘。正如你所看到的,我已经通过设置HorizontalOffset="-150"(它具有固定的弹出宽度(宽度= 300))来实现水平居中。对于垂直放置,我有问题,弹出高度不固定,因为我在其中显示可变大小和纵横比的图像。因此,我试图通过加入VerticalOffset="{Binding ActualHeight}"来设置VerticalOffsetActualHeight。这不起作用。关于我做错了什么以及如何实现我的目标的任何想法?所有的

回答

1

首先你需要一个转换器:

public class MultiplyConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value is double && parameter is double) 
     { 
      return ((double)value) * ((double)parameter); 
     } 

     return value; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

然后尝试将VerticalOffset属性绑定到弹出的孩子ActualHeight

<Window.Resources> 
    <local:MultiplyConverter x:Key="MultiplyConverter" /> 
    <sys:Double x:Key="Factor">-.5</sys:Double> 
</Window.Resources> 

<Popup IsOpen="{Binding PopupViewModel.IsOpen}" 
     Placement="Mouse" 
     HorizontalOffset="-150"> 
    <Popup.VerticalOffset> 
     <Binding Path="Child.ActualHeight" RelativeSource="{RelativeSource Self}" 
        Converter="{StaticResource MultiplyConverter}" ConverterParameter="{StaticResource Factor}" /> 
    </Popup.VerticalOffset> 


    <!-- popup content --> 
</Popup> 

我希望它可以帮助你。