3

我对 WPF 很陌生,仍然试图弄清楚它的所有细节;)我有一个奇怪的问题,按钮没有引发其点击事件。我有一个名为 PopupButton 的 UserControl,它基本上是一个在单击时显示弹出窗口的按钮。弹出窗口的内容是 ContentPresenter,并绑定到 PopupButton 上名为 PopupContentHolder 的依赖属性。然后在主窗体上,我有一个带有 DataGridTemplateColumn 的 DataGrid, <DataGridTemplateColumn.CellTemplate> <DataTemplate>我在其中放置了 PopupButton。然后我分配一个带有 ItemTemplate 的列表框作为我的 PopupButton 的弹出内容。简单来说,这看起来如下

<DataGrid HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        HorizontalContentAlignment="Stretch"
        ColumnHeaderHeight="40"
        FontSize="11"
        HorizontalScrollBarVisibility="Disabled"
        ItemsSource="{Binding Pocos}"
        RowHeight="30"
        SnapsToDevicePixels="True"
        VerticalScrollBarVisibility="Disabled">
<DataGrid.Columns>
    <DataGridTemplateColumn MinWidth="70" Header="NAme">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <controls:PopupButton DoPopupOnMainButton="True"
                                        Foreground="{StaticResource HeaderLinkActiveColor}"
                                        MarkButtonOnPopup="True"
                                        PopupBackground="{StaticResource PopupBackground}"
                                        PopupPadding="5"
                                        ShowDownArrow="False"
                                        Text="Some test button">
                    <controls:PopupButton.PopupContentHolder>
                        <StackPanel>
                            <ListBox Background="Transparent" ItemsSource="{Binding Tenders}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <Button Margin="0,2,0,0"
                                                Click="Button_Click"
                                                Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},
                                                                    Path=DataContext.SaveCommand}"
                                                Content="{Binding .}"
                                                FontSize="11" />
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </StackPanel>
                    </controls:PopupButton.PopupContentHolder>
                </controls:PopupButton>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

现在问题出在PopupButton 中ListBox 内的按钮上,既没有引发Click 事件,也没有引发Command。此外,当我将 PopupButton 放在 DataGrid 之外时,everythinkg 工作得很好。另外,当我将上面的列表框直接放在网格单元格中时,它就可以工作了。也许是我的 PopupButtonControl 出了点问题。

<UserControl x:Class="WpfApplication1.PopupButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         x:Name="userControlRoot"
         Width="Auto"
         Height="Auto">
<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVis" />

    <Storyboard x:Key="PopupCloseStoryBoard">
        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="popup" Storyboard.TargetProperty="IsOpen">
            <DiscreteBooleanKeyFrame KeyTime="0:0:0.1" Value="False" />
        </BooleanAnimationUsingKeyFrames>
    </Storyboard>

    <Storyboard x:Key="PopupOpenStoryBoard">
        <BooleanAnimationUsingKeyFrames Storyboard.TargetName="popup" Storyboard.TargetProperty="IsOpen">
            <DiscreteBooleanKeyFrame KeyTime="0:0:0.0" Value="True" />
        </BooleanAnimationUsingKeyFrames>
    </Storyboard>


</UserControl.Resources>

<Grid>

    <Border x:Name="brdButtonBack"
            Background="Transparent"
            CornerRadius="3,3,0,0"
            Padding="{Binding Padding,
                              ElementName=userControlRoot}" />

    <Button x:Name="btnMain"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Click="btn_Click"
            Command="{Binding MainCommand,
                              ElementName=userControlRoot}"
            Content="{Binding Text,
                              ElementName=userControlRoot}"
            IsEnabled="{Binding IsEnabled,
                                ElementName=userControlRoot}" />

    <Popup x:Name="popup"
           MinWidth="{Binding ActualWidth,
                              ElementName=userControlRoot}"
           AllowsTransparency="True"
           Placement="Bottom"
           PlacementTarget="{Binding ElementName=brdButtonBack}"
           PopupAnimation="Slide"
           StaysOpen="False">
        <Border Background="{Binding PopupBackground,
                                     ElementName=userControlRoot}"
                BorderBrush="White"
                CornerRadius="0,3,3,3"
                Padding="{Binding PopupPadding,
                                  ElementName=userControlRoot}">

            <ContentPresenter Content="{Binding PopupContentHolder, ElementName=userControlRoot}" />
        </Border>
    </Popup>

</Grid>

我已经简化了控制,但该版本也重现了问题。控件背后的代码大多包含依赖属性,唯一的逻辑如下所示

public partial class PopupButton : UserControl
{
    //... dependency properties

    public PopupButton()
    {
        InitializeComponent();
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
            Storyboard s = (Storyboard)TryFindResource("PopupOpenStoryBoard");
            s.Begin();
    }

    private void popup_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        //trick to close popup when the button inside is clicked
        //this must be done with storyboard and delay
        //since popup can not be closed before the event reaches clicked
        //button
        if (e.Source is Button)
        {
            Storyboard s = (Storyboard)TryFindResource("PopupCloseStoryBoard");
            s.Begin();
        }
    }
}

有没有人知道它有什么问题?谢谢。

4

2 回答 2

1

我终于通过将 PopupButton 重写为 CustomControl 来解决这个问题。在那里,当单击包含的按钮时,我没有触发 StoryBoard 来关闭弹出窗口,而是使用了以下代码

mainPopup.AddHandler(Button.ClickEvent, new RoutedEventHandler(mainPopup_ButtonClick), true);

为主弹出窗口添加按钮单击处理程序,即使事件已由源按钮处理,也会在弹出窗口上调用该处理程序。

    void mainPopup_ButtonClick(object sender, RoutedEventArgs e)
    {
        MainPopup.IsOpen = false;
    }

有关在此处处理已处理的路由事件的更多信息

于 2013-12-06T17:55:23.463 回答
0

也许这只是一个错字,因为您的代码中有一个 btn_Click 和 Button_Click 事件处理程序。您介意提供 PopupContentHolder 或整个 PopupButton 类的代码吗?

于 2012-10-09T12:42:27.360 回答