Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果bool为true,则返回int_C#_Wpf_Xaml_Boolean - Fatal编程技术网

C# 如果bool为true,则返回int

C# 如果bool为true,则返回int,c#,wpf,xaml,boolean,C#,Wpf,Xaml,Boolean,根据选中的单选按钮,我需要一个int-bloodtype等于一个特定值。如果任何布尔值为真,它将使血型等于一个特定值。我只是不知道如何让布尔给int bloodtype赋值。有什么建议吗 private void btnAddPatient_Click(object sender, RoutedEventArgs e)////Adds Patients using buttone etc to set properties { string name = txtPatie

根据选中的单选按钮,我需要一个int-bloodtype等于一个特定值。如果任何布尔值为真,它将使血型等于一个特定值。我只是不知道如何让布尔给int bloodtype赋值。有什么建议吗

private void btnAddPatient_Click(object sender, RoutedEventArgs e)////Adds Patients using buttone etc to set properties
    {
        string name = txtPatientName.Text;
        int bloodType,x=1;           
         DateTime dob;
        bool bloodA = rbA.IsChecked.Equals(true);
        bool bloodB = rbB.IsChecked.Equals(true);
        bool bloodAB = rbAB.IsChecked.Equals(true);
        bool blood0 = rb0.IsChecked.Equals(true);



        if (dpDOB.SelectedDate == null || txtPatientName.Text == "" || !bloodA || !bloodAB || !bloodB || !blood0)
        {

            if (txtPatientName.Text == "")
            {
                MessageBox.Show("Please enter Patient's Name");
            }

            else if (dpDOB.SelectedDate == null)
            {
                MessageBox.Show("Please select a date");
            }


            else if(!bloodA || !bloodAB || !bloodB || !blood0)
            {
                MessageBox.Show("Please enter patient's blood type");
            }

        }

        else
        {
            //bloodType  How to make this equal to a value depending on what radio button is checked?
            dob = dpDOB.SelectedDate.Value;

            Patient patient = new Patient(name, bloodType, x, dob);

            MainWindow mainWindow = Owner as MainWindow;

            patients.Add(patient);
            lstPatients.ItemsSource = null;
            lstPatients.ItemsSource = patients;
            // this.Close();
        }
    }

检查每个单选按钮是否正确,并指定正确的值。
大概是这样的:

            else
            {
                //bloodType  How to make this equal to a value depending on what radio button is checked?
                if(bloodA)
                {
                    bloodType = 0;
                }
                else if(bloodB)
                {
                    bloodType = 1;
                }
            }

检查每个单选按钮是否正确,并指定正确的值。
大概是这样的:

            else
            {
                //bloodType  How to make this equal to a value depending on what radio button is checked?
                if(bloodA)
                {
                    bloodType = 0;
                }
                else if(bloodB)
                {
                    bloodType = 1;
                }
            }

如果您不是嵌套ifs的朋友,那么另一个解决方案是将int值指定给每个单选按钮的Tag属性。然后,将组框中的所有按钮分组,找出选中的按钮,最后将标记内容强制转换为int

int i;
var checkedButton = groupBox1.Controls
                .OfType<RadioButton>()
                .FirstOrDefault(rb => rb.Checked);            
if (int.TryParse(checkedButton.Tag.ToString(), out i))
{
    // here's your value 
}
inti;
var checkedButton=groupBox1.控件
第()类
.FirstOrDefault(rb=>rb.Checked);
if(int.TryParse(checkedButton.Tag.ToString(),out i))
{
//这是你的价值
}

如果您不是嵌套ifs的朋友,那么另一种解决方案是将int值分配给每个单选按钮的Tag属性。然后,将组框中的所有按钮分组,找出选中的按钮,最后将标记内容强制转换为int

int i;
var checkedButton = groupBox1.Controls
                .OfType<RadioButton>()
                .FirstOrDefault(rb => rb.Checked);            
if (int.TryParse(checkedButton.Tag.ToString(), out i))
{
    // here's your value 
}
inti;
var checkedButton=groupBox1.控件
第()类
.FirstOrDefault(rb=>rb.Checked);
if(int.TryParse(checkedButton.Tag.ToString(),out i))
{
//这是你的价值
}
首先,我建议提取类型,确切地说,
enum

  // [Flags] // you may want declare enum as Flags
  public enum BloodType {
     O = 0,
     A = 1,
     B = 2,
    AB = 3
  }
然后,您可以在三元运算符的帮助下分配
血型
值:

  BloodType bloodType = 
      rb0.IsChecked ? BloodType.O 
    : rbA.IsChecked ? BloodType.A
    : rbB.IsChecked ? BloodType.B
    : BloodType.AB;  
如果要获取
int
值,只需强制转换:

 int v = (int) bloodType;

 BloodType t = (BloodType) v;
首先,我建议准确地提取类型,
enum

  // [Flags] // you may want declare enum as Flags
  public enum BloodType {
     O = 0,
     A = 1,
     B = 2,
    AB = 3
  }
然后,您可以在三元运算符的帮助下分配
血型
值:

  BloodType bloodType = 
      rb0.IsChecked ? BloodType.O 
    : rbA.IsChecked ? BloodType.A
    : rbB.IsChecked ? BloodType.B
    : BloodType.AB;  
如果要获取
int
值,只需强制转换:

 int v = (int) bloodType;

 BloodType t = (BloodType) v;

有很多方法可以做到这一点。这里还有一个:

更喜欢强数据类型。使用枚举覆盖整数。

public enum BloodTypeEnum
{
    TypeO = 0,
    TypeA = 1,
    TypeB = 2
}
将实例添加到ViewModel中

public class MainViewModel : ViewModelBase
{
    private BloodTypeEnum _bloodType;

    public BloodTypeEnum BloodType
    {
        get { return _bloodType; }
        set
        {
            _bloodType = value;
            RaisePropertyChanged();
        }
    }
}
使用强数据类型和值转换器编写XAML

<StackPanel>
    <StackPanel.Resources>
        <local:EnumToBoolConverter x:Key="EnumToBoolConverter"></local:EnumToBoolConverter>
    </StackPanel.Resources>
    <StackPanel>
        <RadioButton IsChecked="{Binding BloodType, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:BloodTypeEnum.TypeO}, Mode=TwoWay}">Type O</RadioButton>
        <RadioButton IsChecked="{Binding BloodType, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:BloodTypeEnum.TypeA}, Mode=TwoWay}">Type A</RadioButton>
        <RadioButton IsChecked="{Binding BloodType, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:BloodTypeEnum.TypeB}, Mode=TwoWay}">Type B</RadioButton>
    </StackPanel>
</StackPanel>
public class EnumToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return object.Equals(value, parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return System.Convert.ToBoolean(value) ? parameter : DependencyProperty.UnsetValue;
    }
}

有很多方法可以做到这一点。这里还有一个:

更喜欢强数据类型。使用枚举覆盖整数。

public enum BloodTypeEnum
{
    TypeO = 0,
    TypeA = 1,
    TypeB = 2
}
将实例添加到ViewModel中

public class MainViewModel : ViewModelBase
{
    private BloodTypeEnum _bloodType;

    public BloodTypeEnum BloodType
    {
        get { return _bloodType; }
        set
        {
            _bloodType = value;
            RaisePropertyChanged();
        }
    }
}
使用强数据类型和值转换器编写XAML

<StackPanel>
    <StackPanel.Resources>
        <local:EnumToBoolConverter x:Key="EnumToBoolConverter"></local:EnumToBoolConverter>
    </StackPanel.Resources>
    <StackPanel>
        <RadioButton IsChecked="{Binding BloodType, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:BloodTypeEnum.TypeO}, Mode=TwoWay}">Type O</RadioButton>
        <RadioButton IsChecked="{Binding BloodType, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:BloodTypeEnum.TypeA}, Mode=TwoWay}">Type A</RadioButton>
        <RadioButton IsChecked="{Binding BloodType, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:BloodTypeEnum.TypeB}, Mode=TwoWay}">Type B</RadioButton>
    </StackPanel>
</StackPanel>
public class EnumToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return object.Equals(value, parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return System.Convert.ToBoolean(value) ? parameter : DependencyProperty.UnsetValue;
    }
}

你不能用
if else if
检查一下,哪个单选按钮是true并分配正确的值吗?血型的
int
值在哪里定义?是的,我可以。天哪,我当时很笨。我认为一个聪明的方法是使用枚举,因为我不知道你将每个血型映射到的整数值。想分享你的枚举方法吗?我希望血型等于0,1,2,3你能用
if else if
检查一下吗?哪个单选按钮为true并指定正确的值?血型的
int
值在哪里定义?是的,我可以。天哪,我当时很笨。我认为一个聪明的方法是使用枚举,因为我不知道你将每个血型映射到的整数值。想分享你的枚举方法吗?我希望血型等于0,1,2,3比我快。绝对是最简洁的方法。。。您可以定义
public enum BloodType{O,A,B,AB}
和您的
BloodType BloodType=rb0.checked在单行上为偶数。第三行是把血型转换成整数,比我快。绝对是最简洁的方法。。。您可以定义
public enum BloodType{O,A,B,AB}
和您的
BloodType BloodType=rb0.checked在单行上为偶数。第三行将血型转换为int。