我正在研究WPF用户界面。我在DataGrid中有一个Popup,它在TextBlock的“MouseLeftButtonDown”事件中打开,并询问用户是否确定要删除该行。在那之前一切正常。 问题是,如果我单击datagrid的标题行,Popup只会关闭。如果我点击其他地方弹出窗口没有关闭。属性“StaysOpen”已设置为false。 如果我在Datagrid外部使用相同的XAML代码,一切正常。
如何实现弹出窗口关闭? 顺便说一下,我正在使用MVVM模式,所以如果将解决方案集成到XAML代码中会很棒。
我的XAML代码:
<UserControl.Resources>
<ResourceDictionary>
<Storyboard
x:Key="OpenDeleteReadingPopup"
BeginTime="0:0:0">
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="DeleteReadingChoicesPopup"
Storyboard.TargetProperty="IsOpen">
<DiscreteBooleanKeyFrame
KeyTime="00:00:00"
Value="true" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</ResourceDictionary>
</UserControl.Resources>
<StackPanel x:Name="OuterElement">
<toolkit:DataGrid
ItemsSource="{Binding Readings}"
SelectedItem="{Binding SelectedReading}"
IsReadOnly="False">
<toolkit:DataGrid.Columns>
<!-- other column-->
<toolkit:DataGridTemplateColumn>
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock
x:Name="DeleteReadingTextBlock"
Margin="0 1 0 0"
FontFamily="Marlett"
Text="r"
HorizontalAlignment="Right"
VerticalAlignment="Stretch">
<TextBlock.Triggers>
<EventTrigger
RoutedEvent="MouseLeftButtonDown">
<BeginStoryboard
Storyboard="{StaticResource OpenDeleteReadingPopup}" />
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
<Popup
AllowsTransparency="True"
x:Name="DeleteReadingChoicesPopup"
StaysOpen="False"
Placement="Top"
PlacementTarget="{Binding ElementName=DeleteReadingTextBlock}">
<StackPanel
Background="{DynamicResource WindowBackgroundColorBrush}"
HorizontalAlignment="Center">
<Button
x:Name="Button"
cal:Action.TargetWithoutContext="{Binding ElementName=OuterElement, Path=DataContext}"
Style="{DynamicResource DeleteButtonStyle}"
cal:Message.Attach="DeleteReading($dataContext)"
Content="{lex:Loc Sure}"/>
</StackPanel>
</Popup>
</StackPanel>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</StackPanel>