覆盖基类中定义的属性 [英] Override a property defined in base class
问题描述
我有情况下的类层次结构是这样的,
I have case where the class hierarchy is something like this,
+---------------+
| UIElement |
|---------------| +----------------------+
| ... | | My Windows Application
| SomePropert{} | |----------------------|
| |<---+ |+--------------------+|
| | | ||MyUserControl ||
+---------------+ | ||--------------------||
+--------------+-----+ || ||
|FrameWorkElement | |+--------------------+|
|--------------------| |//Want to use |
| ... |<-+ |// SomeProperty; |
+--------------------+ | | |
+-----------+-+ | |
|Control | | |
|-------------| +----------------------+
| ... |<---+
+-------------+ |
+-----------+---+
| UserControl |
|---------------|<---+
| ... | |
+---------------+ |
+----------+-------+
| MyUserControl |
|------------------|
| SomeProperty{} |
| //Want to override
| |
+------------------+
在我的应用程序(和所有其他应用程序,我可以导出此的MyUserControl)
现在我可以设置 SomeProperty
由该处理的MyUserControl
类,而不是的UIElement
?
Now in my app (and all other apps where I can export this MyUserControl) can I set the SomeProperty
that is handled by the MyUserControl
class rather than UIElement
?
我现在通过创建一个对象来这样做和的MyUserControl分配,要我在我的XAML page.So加入现在看起来是这样的控制,
I am right now doing this by creating an object to MyUserControl and assigning that to the control that I added in my xaml page.So right now looks like this,
MyUserControl newControl = new MyUserControl();
web = windowsPhoneControl11; //windowsPhoneControll1 is the
//one that I added from toolbox.
// i.e., mycustomecontrol added from toolbox.
所以,现在,因为我使用的是新它被覆盖。但是,当我出口这种控制我不能指望用户创建一个新的对象,一个是在XAML页面中使用它分配给控件。
So now since I am using the 'new' it gets overriden. But when I export this control I can't expect the user to create a new object and assign it to the control that one is using in the xaml page.
那么,有没有这样的财产分配由类的MyUserControl而不是的UIElement类处理,我可以覆盖此一个属性的任何其他方式?我的意思大概具有控制来设置该属性的MyUserControl是,我需要分配给它之前检查一些价值。如果不是ATLEAST预期值,那么我需要将其设置为默认值
So is there any other way I could override this one property so that the assignment of that property is handled by MyUserControl class rather than the UIElement class? What I mean about MyUserControl having the control to set this property is that I need to check for some value before assigning it. If it is not atleast an expected value then I need to set it to a default value.
PS:我是这么长的问题,抱歉,我无法表达它更精确,我没能找到与此相关的电气特性的问题。它是WindowsPhoneApp ......不是普通windowsapplication。
Ps: I am sorry for such a long question but I couldn't express it more precise and I was not able to find anyother question related to this. And it is WindowsPhoneApp... Not the ordinary windowsapplication.
推荐答案
原谅我,如果我理解正确这,但会以下工作方式:
Forgive me if I've interpreted this incorrectly but would the following work:
public class BaseClass
{
public int MyProperty
{
get; set;
}
}
public class ChildClass : BaseClass
{
public new int MyProperty
{
get
{
return base.MyProperty;
}
set
{
if(DoYourCheckingStuff(value))
{
base.MyProperty = value;
}
}
}
}
没't检验这一点。
虽然这种感觉就像做一个真正破解杂交的方式。什么属性,你实际上是试图有控制了吗?因为可能有这样做的更简单的方法
一个例子:更改用户控件,以便它的宽度不能100和200之间进行设置(虽然这可能是一个非常糟糕的方式做到这一点),通过隐藏它的宽度属性:
Didn't test this.
Although this feels like a really hack-ish way of doing it. What property are you actually trying to 'have control' over? Since there may be easier ways of doing this.
An example: Change a UserControl so that it's width can't be set between 100 and 200 (Although this is probably a pretty bad way to do it), by hiding it's Width property:
public class MyUserControl : UserControl
{
public new double Width
{
get
{
return base.Width;
}
set
{
if(!(value > 100 && value < 200))
base.Width = value;
}
}
}
这篇关于覆盖基类中定义的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!