我有一个在C#中动态创建的按钮。现在我希望此按钮在单击时隐藏。我该怎么做?
我试过这个,但它不起作用:
if (hide_button.Click == true)
{
hide_button.Visibility = Visibility.Hidden;
}
答案 0 :(得分:1)
我不确定以下行是否适合您。
if (hide_button.Click==true)
尝试删除此行,它将起作用。因为事件不等于布尔值。
这段代码对我有用
Button buton = new Button();
public MainWindow()
{
InitializeComponent();
buton.Click += Buton_Click;
grid.Children.Add(buton);
}
private void Buton_Click(object sender, RoutedEventArgs e)
{
buton.Visibility = Visibility.Hidden;
}
答案 1 :(得分:0)
button.Click += (sender,e) =>
{
if (button.Visibility == Visibility.Visible)
button.Visibility = Visibility.Collapsed;
else
button.Visibility = Visibility.Visible;
};
或
button.Click += new EventHandler(button_Click);
private void button_Click(object sender, EventArgs e)
{
if (button.Visibility == Visibility.Visible)
button.Visibility = Visibility.Collapsed;
else
button.Visibility = Visibility.Visible;
}