当Popup包含在UserControl中时,弹出窗口中包含的控件无法接收鼠标单击

时间:2011-11-29 03:41:21

标签: wpf

我有UserControl,格式如下:

<UserControl x:Class="apparition2.Tabs.Scene.SceneMultiPicker"
             x:Name="control"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
             xmlns:local="clr-namespace:apparition2.Tabs.Scene">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <TextBlock x:Name="PART_SelectedItems" HorizontalAlignment="Stretch" />
        <Button x:Name="PART_Button" Grid.Column="1" Width="20" Click="PART_Button_Click" />

        <Popup x:Name="PART_Popup" Width="200" Height="200" AllowsTransparency="True"
               PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
               HorizontalOffset="5" VerticalOffset="5" Placement="Left" PlacementTarget="{Binding ElementName=PART_Button}">
            <mwt:SystemDropShadowChrome Color="#71000000" Margin="0,0,5,5">
                <Border Background="White" BorderBrush="Black" BorderThickness="1" CornerRadius="3" Padding="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <TextBox HorizontalAlignment="Stretch" Margin="0,0,0,5" />

                        <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Visible"
                                      Grid.Row="1">
                            <ItemsControl x:Name="PART_Items" ItemsSource="{Binding ElementName=control, Path=Items}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox Margin="5,3" Content="{Binding Name}" IsChecked="{Binding IsSelected, Mode=TwoWay} />
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </ScrollViewer>
                    </Grid>
                </Border>
            </mwt:SystemDropShadowChrome>
        </Popup>
    </Grid>
</UserControl>

PART_Button点击处理程序将PART_Popup.IsOpen设置为TrueItemsControl中的PART_Popup会显示许多行的复选框。但是,TextBox中包含的CheckBoxsPART_Popup无法获得焦点。

WPF默认主题为这些控件提供鼠标悬停反馈 - TextBox的边框亮起,CheckBox表面发光。但点击它们什么都不做。通过使用简单的PART_Popup替换<Button>的内容,绑定已被隔离为不影响。这也会收到鼠标悬停反馈,但无法点击。

如果将此相同的模板内嵌到<Window>而不是UserControl,则可以正常使用。是什么给了什么?

1 个答案:

答案 0 :(得分:2)

要使Popup接收焦点中包含的子控件(如TextBoxes和CheckBoxes)尝试在Visual Tree父元素中设置Focusable="False"