2016-10-04 50 views
1

我有一個ContentControl風格,其中包含一個包裝文本框的彈出窗口。我知道這聽起來有點令人困惑,但我會在下面發佈一些代碼。顯示彈出窗口上的大寫鎖定鍵時,但當窗口被拖動時,彈出窗口不隨之移動。彈出式ContentControl不更新的風格

我需要弄清楚如何更新他們風格的彈出窗口的位置。

這個ContentControl用在窗口和UserControl上,所以這就是爲什麼我試圖在樣式中解決這個問題。

這個問題不同於其他的一些,因爲我試圖在風格而不是代碼中解決它。

內容控制:

public class ShowCapLockWarningControler : ContentControl 
{ 

    static ShowCapLockWarningControler() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(ShowCapLockWarningControler), new FrameworkPropertyMetadata(typeof(ShowCapLockWarningControler))); 
    } 

    public static readonly DependencyProperty ShowMessageProperty = 
     DependencyProperty.Register(Reflection.GetPropertyName<ShowCapLockWarningControler>(i => i.ShowMessage), typeof(bool), 
            typeof(ShowCapLockWarningControler), new PropertyMetadata(false)); 

    public bool ShowMessage 
    { 
     get { return (bool)GetValue(ShowMessageProperty); } 
     set { SetValue(ShowMessageProperty, value); } 
    } 

    public ShowCapLockWarningControler() 
    { 
     IsKeyboardFocusWithinChanged += (s, e) => RecomputeShowMessage(); 
     PreviewKeyDown += (s, e) => RecomputeShowMessage(); 
     PreviewKeyUp += (s, e) => RecomputeShowMessage(); 
    } 

    private void RecomputeShowMessage() 
    { 
     ShowMessage = IsKeyboardFocusWithin && Console.CapsLock; 
    } 
} 

如何使用它:

<controls:ShowCapLockWarningControler Grid.Row="1" Grid.Column="2" Style="{DynamicResource CaplockWarning}"> 
    <PasswordBox Width="150" Name="PasswordBox" PasswordChanged="HandlePasswordChanged" VerticalContentAlignment="Center" 
       KeyDown="HandlePasswordBoxEnterPressed"/> 
</controls:ShowCapLockWarningControler> 

在樣式字典中的風格:

<Style x:Key="CaplockWarning" TargetType="{x:Type controls:ShowCapLockWarningControler}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type controls:ShowCapLockWarningControler}"> 
       <Grid> 
        <ContentPresenter Name="Presenter"/> 
        <Popup Placement="Bottom" PlacementTarget="{Binding ElementName=Presenter}" Name="BalloonPopup" AllowsTransparency="True" 
          IsOpen="{TemplateBinding ShowMessage}" > 
         <!-- Visual of the popup-->      
        </Popup> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

我期待照顧這在風格,因爲沒有地方落後和綁定添加代碼。 –

+0

你爲什麼要保持彈出。關閉丟失的焦點。 – Versatile

回答

0

使用Window.LocationChanged eventPopup.Placement = AbsolutePointPopup.HorizontalOffsetPopup.VerticalOffset

在下面的示例中,當ContentControl'sLoaded事件被觸發時出現Popup。當Window更改其位置時,我們更改相關的Popup屬性。

代碼:

<ContentControl x:Name="CntCtrl" Height="35" Content="Some content" Loaded="CntCtrl_Loaded_1"/> 

<Popup PlacementTarget="{Binding ElementName=CntCtrl}" Placement="AbsolutePoint" x:Name="Popup1"> 
    <ListBox> 
     <ListBoxItem>item1</ListBoxItem> 
     <ListBoxItem>item1</ListBoxItem> 
     <ListBoxItem>item1</ListBoxItem> 
     <ListBoxItem>item1</ListBoxItem> 
     <ListBoxItem>item1</ListBoxItem> 
     <ListBoxItem>item1</ListBoxItem> 
     <ListBoxItem>item1</ListBoxItem> 
    </ListBox> 
</Popup> 

代碼:

private void Window_LocationChanged_1(object sender, EventArgs e) 
{   
    Point ptb = CntCtrl.PointToScreen(new Point(0, 0)); 
    Popup1.HorizontalOffset = ptb.X; 
    Popup1.VerticalOffset = ptb.Y + CntCtrl.Height; 
} 

private void CntCtrl_Loaded_1(object sender, RoutedEventArgs e) 
{ 
    Popup1.IsOpen = true; 
    Point ptb = CntCtrl.PointToScreen(new Point(0, 0)); 
    Popup1.HorizontalOffset = ptb.X; 
    Popup1.VerticalOffset = ptb.Y + CntCtrl.Height; 
} 
+0

它不完全在風格,但它會做。謝謝。 –