I have Simple application.
<Window x:Class="WpfControlReview.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/开发者_运维知识库markup-compatibility/2006" Height="120" Width="277" ResizeMode="NoResize">
<Grid>
<Button HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Stretch" HorizontalContentAlignment="Center" Click="button1_Click">
<StackPanel HorizontalAlignment="Stretch" Name="stackPanel1" VerticalAlignment="Stretch" Orientation="Vertical">
<Label IsHitTestVisible="False" Content="Select your options and press to commit" Name="label1" HorizontalContentAlignment="Center" FontSize="12" />
<StackPanel Name="stackPanel2" Orientation="Horizontal">
<Expander Header="Color" x:Name="expander1"/>
<Expander Header="Make" x:Name="expander2"/>
<Expander Header="Payment" x:Name="expander3"/>
</StackPanel>
</StackPanel>
</Button>
</Grid>
</Window>
private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
Application.Current.Shutdown();
}
When I click button application closes. That is what I want. But when I click on one of the expanders application also closes. How to avoid that? I don't want application to close when user clicks on one of three arrows.
Try checking the source for the click (as Click events bubble up until someone explicitly handles them). And because the Button is a parent of the expander, it will receive its click event too (did you mean to put the expander outside the button?):
private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (e.Source == this.button1)
{
Application.Current.Shutdown();
}
}
精彩评论