2016-06-10 68 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> 

我希望它可以幫助你。