我对 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();
}
}
}
有没有人知道它有什么问题?谢谢。