【问题标题】:Popup does not close when scrolling滚动时弹出窗口不关闭
【发布时间】:2012-06-07 00:34:38
【问题描述】:

我有一个文本框,当它获得焦点时,会在其下方显示一个弹出窗口。但是当弹出窗口打开并且我滚动时,它似乎停留在打开它的同一个位置。我需要帮助来弄清楚当我滚动时如何让弹出窗口出现在文本框下方。我将如何在 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>

【问题讨论】:

标签: c# wpf xaml popup scroll


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多