在WPF中,每次打勾或写东西时,我如何检查条件是否满足
本文关键字:何检查 满足 是否 条件 检查 WPF | 更新日期: 2023-09-27 18:19:26
基本上,我尝试过在大学制作益智游戏/解决额外学分的代码。但现在,我做这件事的方式(因为缺乏其他方式)似乎很迟钝,显然不被推荐。
每次他们勾选复选框或在文本框中输入内容时,我都希望它检查条件是否满足。
//the conditions
if ((txbName.Text == "Tim") && (txbFamName.Text == "Dams") && (txbLocation.Text == "AP") && (txbPassword.Text == "C#") && (cbxThird.IsChecked == true) && (cbxA.IsChecked == true) && (cbxApple.IsChecked == true))
MessageBox.Show("You solved the puzzle!");
这是我的XAML文件
https://gist.github.com/themaawaa/70f11afa6a791c0f49f0
这是我的代码
https://gist.github.com/themaawaa/4703a7a4a06e4787ef16
与其有几十个Click事件,不如尝试使用数据绑定,这正是WPF的初衷。通过这种方式,您可以将复选框绑定到布尔属性,并实际降低代码和视图的复杂性,还有:单选按钮
<TextBox Name="txtbx1" Text="{Binding TextBoxPropertyName, UpdateSourceTrigger=PropertyChanged}" Width="150" />
在你身后的代码中:
private string _textBoxPropertyName;
public string TextBoxPropertyName {
/*your get and set logic here*/
get{return _textBoxPropertyName;}
set{CheckForWinCondition(); _textBoxPropertyName = value;}
}