2012-03-09 295 views
3

我有一個文本框,當它有焦點時彈出窗口出現在它下面。但是當彈出窗口打開並且我滾動時,它似乎停留在它打開的同一個位置。我需要幫助確定如何在滾動時在文本框下方顯示彈出窗口。我將如何在xaml中做到這一點?彈出窗口不滾動時關閉

謝謝!

主窗口視圖:

<Grid x:Name="LayoutRoot"> 
    <ScrollViewer> 
     <local:ControlView/> 

    </ScrollViewer> 
</Grid> 

資源詞典:

<Style TargetType="{x:Type Popup}"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding ElementName=txtTest, Path=IsKeyboardFocused}" Value="True"> 
      <Setter Property="IsOpen" Value="True" /> 
     </DataTrigger> 
     <DataTrigger Binding="{Binding ElementName=txtTest, Path=IsKeyboardFocused }" Value="False"> 
      <Setter Property="IsOpen" Value="False" /> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 
<Style x:Key="BorderStyle" TargetType="{x:Type Border}"> 
    <Setter Property="Background" Value="LemonChiffon"/> 
    <Setter Property="Padding" Value="5"/> 
</Style> 

用戶控件查看:

<Grid x:Name="LayoutRoot"> 
     <StackPanel Grid.Row="1" Grid.Column="4" Orientation="Vertical"> 
         <Button Content="Button" Width="100" Height="100"/> 
         <Button Content="Button" Width="100" Height="100"/> 
         <TextBox x:Name="txtTest" HorizontalAlignment="Stretch"/> 

         <Popup Name="TestPopup" StaysOpen="True" 
          PlacementTarget="{Binding ElementName=txtTest}" 
          AllowsTransparency="True" Placement="{Binding ElementName=txtTest}"> 
          <Border Style="{DynamicResource BorderStyle}"> 
           <GroupBox Header="Test Popup"> 
            <ScrollViewer Margin="0,2,0,0" VerticalScrollBarVisibility="Hidden"> 
            <!--BINDING--> 
            <Label Content="Hello World!"/>        
            </ScrollViewer> 
           </GroupBox> 
          </Border> 
         </Popup> 
         <ContentPresenter Content="{Binding testBinding}" 
              VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
         </ContentPresenter> 
        </StackPanel> 
</Grid> 
+3

你能發表一些代碼嗎?像什麼容器是文本框? – 2012-03-09 19:45:08

+0

您已閱讀過http://stackoverflow.com/questions/2309883/wpf-detect-scrolling-parent-control? – 2012-03-11 02:08:20

+0

您應該在打開時提供彈出式對焦。這種方式你不應該能夠滾動,除非彈出關閉。我發現的最好方法是看看組合框控件模板如何在msdn上工作:http://msdn.microsoft.com/en-us/library/ms752094(v=vs.90).aspx – Silvermind 2012-03-11 17:29:12

回答

1

試試這個:

<Style TargetType="{x:Type Popup}"> 
    <Setter 
     Property="IsOpen" 
     Value="{Binding IsKeyboardFocusedWithin, ElementName=txtTest, Mode=OneWay}"/> 
</Style>