开发者

Is is possible to Create an Alias of a Form Control by Changing the name property

Suppose I have a ComboBox with name, myComboBox.

Now, if I declare another ComboBox reference, say:

ComboBox curBox=new ComboBox();
curBox.Name = curCombo; // curCombo is a string whose value开发者_开发技巧 is myComboBox.

Will curBox then be an alias for, that is directly refer to myComboBox.


The name property of a WPF control is just used to fill the name attribute in your XAML markup. It should be unique as it provides a (high-level) reference to your control (you can find your control by name).

In your example, myComboBox and curBox are two separate instances of a ComboBox, who will simply share the same name. This is not allowed as names should be unique within a Namescope. See here for more on this subject.


Rewinder is right, just want to add: setting curBox.Name in code does not automatically add this control to the current Namescope. So, setting it should not be a problem. For adding it to the namescope you would use RegisterName which will throw an ArgumentException if the "name provided would result in a duplicate name registration".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜