如何在更改时根据另一个属性的值设置多个属性值。 [英] How do I set multiple property values based on the value of another property when it changes.
问题描述
从我所看到的,从另一个属性设置其他属性值应该是不好的做法。我需要做的是确保特定属性何时更改,相关属性也会更改,而不必更改它。
例如,如果我有以下内容属性:
From what I have read it is supposed to be bad practice to be setting other property values from another property. What I need to do is handle making sure when a specific property changes that the related property changes as well without having to change it.
For instance if I had the following properties:
private bool connected;
private bool disconnected;
private bool ready;
private bool noLiveCalls;
我设置
And I set
noLiveCalls = false;
我需要将以下属性设置为
I need the following properties to be set to
connected = true;
disconnected = false;
ready = false;
如果我设置
If I set
ready = true;
然后这些也需要设置
Then these would need to be set also
noLiveCalls = true;
connected = false;
disconnected = false;
所以这样的事情应该是一种不好的做法,但我似乎无法看到另一种方式。
So something like this is supposed to be a bad practice, however I can not seem to see another way around it.
private bool ready;
public bool Ready {
get {
return ready;
}
set {
if (ready != value) {
if (value)
{
noLiveCalls = true;
connected = false;
disconnected = false;
}
ready = value;
}
}
}
这是一个不好的做法,如果有的话关于如何以另一种方式处理它的建议?
我尝试了什么:
我尝试过使用事件和propertychanged但它们似乎不是我需要的全部内容,试图避免单独管理某些属性。
我搜索了codeproject和谷歌,但没有找到我正在寻找的东西,但也许我写错了搜索或使用错误的关键字。我确实需要其他地方的属性来检查其他人的值以允许或阻止其他属性发生变化。
So is this a bad practice, and if it is any suggestions as to how I can handle it another way?
What I have tried:
I have tried using events and propertychanged but they do not seem be what I need the whole point being to try and avoid having to manage certain properties individually.
I have searched through codeproject and google but have not found what I am looking for but perhaps I am wording the search wrong or using the wrong keywords. I do need the properties in other places to check their values against others to allow or prevent another property from changing.
推荐答案
我像你一样处理这类情况。所以,这是正确的。无需担心。
I handle these kind of situations like you did. So, it's correct. No need to worry.
这篇关于如何在更改时根据另一个属性的值设置多个属性值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!