A
A
AxlDeLarge2014-03-17 11:27:10
Programming
AxlDeLarge, 2014-03-17 11:27:10

Which element needs to suppress the event in order for everything to work correctly?

It is required using Grid and StackPanel to create a form consisting of three TextBlocks.
Create MouseLeftButtonDown events for textBlocks, StackPanel, Grid and Window.
When clicking on "textblock 1" with a bubble event, "textblock1 + stackpanel + grid" should be displayed in place of the TextBlock. When clicking on "textBlock 2" - only "textblock2 + grid + window". Only "textblock 1" works correctly, and when you click on "textBlock 2" it gives out "textblock2 + stackpanel + grid + window". How to make it display only "textblock2 + grid + window"?

Title="MainWindow" Height="350" Width="525" MouseLeftButtonDown="Window_MouseLeftButtonDown">
        <Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
        <StackPanel x:Name="st1" MouseLeftButtonDown="st1_MouseLeftButtonDown">
            <Grid Height="320">
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="1" Grid.Row="0"
                   Text="TextBlock" x:Name="tbResult"/>
            <TextBlock Grid.Column="0" Grid.Row="2"
                   Text="textBlock 1" x:Name="tb1" MouseLeftButtonDown="tb1_MouseLeftButtonDown"/>
            <TextBlock Grid.Column="2" Grid.Row="2"
                   Text="textBlock 2" x:Name="tb2" MouseLeftButtonDown="tb2_MouseLeftButtonDown"/>
        </Grid>
    </StackPanel>
</Grid>

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tbResult.Text += " + window";
        }
        private void st1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tbResult.Text += " + stackpanel";
        }
        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tbResult.Text += " + grid";
            if(((TextBlock)e.OriginalSource).Name == "tb1")
            {
                e.Handled = true;
            }            
        }
        private void tb1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tbResult.Text = "textBlock1";
        }
        private void tb2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tbResult.Text = "textBlock2";
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Vishnyakov, 2014-07-24
@asvishnyakov

private void st1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (e.OriginalSource.Name != "tb2")
    {
        tbResult.Text += " + stackpanel";
    }
}

Although I don't understand why you need it. And in general the code is terrible, even mine =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question