[英]c# WPF if statement for a check box and button press
Im looking to write an if statement for when a checkbox is checked and a button is pressed then do something however i can't get the if statement to work.我想为选中复选框并按下按钮时编写一个 if 语句,然后执行某些操作,但是我无法使 if 语句起作用。
This is the code i have so far这是我到目前为止的代码
if (GPBox.IsChecked == true && SearchButton.MouseUp == true )
{
connect = new MySqlConnection(connectionString);
cmd = new MySqlCommand("select distinct * from gpSurgery", connect);
connect.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
connect.Close();
DataGrid1.DataContext = dt;
}
That code should go on to MouseUp event of the button, and then you don't need该代码应该继续到按钮的 MouseUp 事件,然后你不需要
&& SearchButton.MouseUp == true
You could add the mouseup event in xaml where your button is.您可以在按钮所在的 xaml 中添加 mouseup 事件。
In the event handler you would write:在事件处理程序中,您将编写:
if (GPBox.IsChecked == true)
{
connect = new MySqlConnection(connectionString);
cmd = new MySqlCommand("select distinct * from gpSurgery", connect);
connect.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
connect.Close();
DataGrid1.DataContext = dt;
}
Why you not change it instead of the MouseUp
to the Button_Click为什么你不改变它而不是
MouseUp
到 Button_Click
private void Button_Click(object sender, RoutedEventArgs e)
{
if (GPBox.IsChecked == true)
{
connect = new MySqlConnection(connectionString);
cmd = new MySqlCommand("select distinct * from gpSurgery", connect);
connect.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
connect.Close();
DataGrid1.DataContext = dt;
}
}
My testing code XAML我的测试代码 XAML
<StackPanel>
<CheckBox x:Name="GPBox"/>
<Button Click="Button_Click"/>
</StackPanel>
Code behind:代码背后:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (GPBox.IsChecked == true)
MessageBox.Show("222");
}
I hope it is enough to get you started.我希望这足以让你开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.