如何切换bool属性

  • 本文关键字:属性 bool 何切换 c#
  • 更新时间 :
  • 英文 :


切换4个bool的最干净的方法是什么?因此,如果我将Bool2设置为true,所有其他的都被设置为false。类似于单选按钮,但具有以下四个属性。这可以使用三元操作符/逻辑操作符吗?

XAML:

<TextBlock Text=" {Binding Bool1}"></TextBlock>
<TextBlock Text=" {Binding Bool2}"></TextBlock>
<TextBlock Text=" {Binding Bool3}"></TextBlock>
<TextBlock Text=" {Binding Bool4}"></TextBlock>

c#:

bool b1;
public bool Bool1
{
get { return b1; }
set
{
b1 = value;
OnPropertyChanged(nameof(Bool1));
}
}
bool b2;
public bool Bool2
{
get { return b2; }
set
{
b2 = value;
OnPropertyChanged(nameof(Bool2));
}
}
bool b3;
public bool Bool3
{
get { return b3; }
set
{
b3 = value;
OnPropertyChanged(nameof(Bool3));
}
}
bool b4;
public bool Bool4
{
get { return b4; }
set
{
b4 = value;
OnPropertyChanged(nameof(Bool4));
}
}
void Toggle(object ThePropertySetToTrueHere)
{
}

我的建议-一个enum

public class WhatEver
{ 
public enum WhatsTrue{
Thing1,
Thing2,
Thing3,
Thing4
};
WhatsTrue Truth;
void SetTruth(WhatsTrue truth){
Truth = truth;
}
}

而不是-在代码的某个地方

if(Bool1).....

if(!Bool3) ....

if(Truth == WhatsTrue.Thing1) ..

if(Truth != WhatsTrue.Thing3)

最新更新