When I press the button, the Grid with elements moves, but if I press it many times - the Grid goes very far, how can I limit its movement?

The width of the Grid 1700 and half goes beyond the form, its margin -

<Grid Margin="0,0,-722,0"> 

Need to limit to

 <Grid Margin="-722,0,0,0"> 

Tell me how can I do this?

    1 answer 1

    Judging by your previous question in the event handler, you need to check for indentation.

     private void right_Click(object sender, RoutedEventArgs e) { Thickness margin = Block_1.Margin; if (margin.Left <= -722) { margin.Left += 50; this.Block_1.BeginAnimation(FrameworkElement.MarginProperty, new ThicknessAnimation(margin, TimeSpan.FromSeconds(0.10))); } } 
    • Thank you, it turned out, the only problem is that if I press the button until the limit comes and then I press the left button, the Grid does not move, but remains half protruding from the left ... I mean - how can I make the method to the opposite a button? - Elizabeth
    • margin.Left , make a method for another button, only there margin.Left - Gardes
    • Thickness margin = getBlockName.Margin; if (margin.Left <= -722) { margin.Left -= 250; getBlockName.BeginAnimation(FrameworkElement.MarginProperty, new ThicknessAnimation(margin, TimeSpan.FromSeconds(0.25))); } Thickness margin = getBlockName.Margin; if (margin.Left <= -722) { margin.Left -= 250; getBlockName.BeginAnimation(FrameworkElement.MarginProperty, new ThicknessAnimation(margin, TimeSpan.FromSeconds(0.25))); } You mean so? in this case, nothing happens to me at all, when I press the buttons - Elizabeth
    • @ Elizabeth, the condition then needs to be changed to >= . You do not enter the code block as you have already crossed the border - Gardes
    • The problem is only with the left, the right one fixed ... The left button does not return fully the Grid .. `if (margin.Left <= -722) {margin.Left + = 200; getBlockName.BeginAnimation (FrameworkElement.MarginProperty, new ThicknessAnimation (margin, TimeSpan.FromSeconds (0.25))); } ` - Elizabeth