滚动时弹出窗口不会关闭

时间:2012-03-09 19:39:39

标签: c# wpf xaml popup scroll

我有一个文本框,当它有焦点时,它下面会出现一个弹出窗口。但是当弹出窗口打开并滚动时,它似乎停留在打开它的同一位置。当我滚动时,我需要帮助弄清楚如何让弹出窗口出现在文本框下方。我将如何在xaml中执行此操作?

谢谢!

MainWindow查看:

<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>

UserControl查看:

<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>

1 个答案:

答案 0 :(得分:1)

试试这个:

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