我对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();
}
}
}
有谁知道这有什么问题吗?
谢谢。
最佳答案
我确实通过将PopupButton重写为CustomControl来解决了这个问题。当单击包含的按钮时,我使用了以下代码,而不是触发StoryBoard关闭弹出窗口
mainPopup.AddHandler(Button.ClickEvent, new RoutedEventHandler(mainPopup_ButtonClick), true);
为主弹出窗口添加按钮单击处理程序,即使事件已由源按钮处理,该按钮也会在弹出窗口上调用。
void mainPopup_ButtonClick(object sender, RoutedEventArgs e)
{
MainPopup.IsOpen = false;
}
有关处理已处理的路由事件的更多信息here