简体   繁体   中英

C# WPF Can't click button after defining margin

I have created a button in my WPF Application using the following code:

Button EditButton = new Button();
EditButton.Margin = new System.Windows.Thickness(Location[0], Location[1], 0, 0);
EditButton.Height = double.Parse("20");
EditButton.Width = double.Parse("20");
EditButton.Cursor = System.Windows.Input.Cursors.Hand;
EditButton.Content = "TEST!";
EditButton.Click += new System.Windows.RoutedEventHandler(Edit_Click);
Grid.Children.Add(EditButton);
Location[1] += 17;

The button works perfectly when I have not defined EditButton.Margin but as soon as I define it I can't click it and the cursor does not change. I have searched the internet around for an answer and none of them seemed to work. Thanks in advance.

Not sure what 'Location' is in your code, and I assume 'Grid' is the name of the grid. The below works.

public MainWindow()
    {
        InitializeComponent();
        Button EditButton = new Button();
        EditButton.Margin = new System.Windows.Thickness(10, 10, 0, 0);
        EditButton.Height = double.Parse("20");
        EditButton.Width = double.Parse("20");
        EditButton.Cursor = System.Windows.Input.Cursors.Hand;
        EditButton.Content = "TEST!";
        EditButton.Click += new System.Windows.RoutedEventHandler(Edit_Click);
        Grid.Children.Add(EditButton);
      //  Location[1] += 17;
    }

private void Edit_Click(object sender, RoutedEventArgs e)
{
    throw new NotImplementedException();
}

XAML -

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="Grid">

    </Grid>
</Window>

If you cannot click the control you have created, then it is generally being caused by other control being on top of it.

I would suggest altering your code slightly and move on from that point:

var stackPanel = new StackPanel();
var button = new Button();
button.Content = "Your Button";
button.Click += new System.Windows.RoutedEventHandler(Edit_Click);
stackpanel.Children.Add(button);

I suggest using StackPanel as it automatically arrange your control and thus prevents it from overlapping you can start from this point on see whether issue was caused by grid or some other component.

Button will stretch by default to its content, so will StackPanel .

It looks like you want to do this programmatically, but if you define it in XAML, you could set the button's Panel.ZIndex property to some high number to bring it to the front:

<Button Content="TEST!" Panel.ZIndex="1000" Height="20" Width="20" Cursor="Hand" Click="Edit_Click" />

Hope that helps somebody...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM