如何将UserControl中设置为private?

时间:2022-10-19 22:33:08

I have a user control with a ComboBox and a TextBox. Everything is working great except I noticed that from my user control's instance object, I can access those two controls. They shouldn't be accessible except via my own exposed properties.

我有一个带有组合框和文本框的用户控件。一切都很好,除了我注意到从我的用户控件的实例对象,我可以访问这两个控件。它们不应该被访问,除非通过我自己公开的属性。

1 个解决方案

#1


38  

You can use the x:FieldModifier attribute on your controls, thusly:

您可以在控件上使用x:FieldModifier属性,如下所示:

<TextBox x:Name="textBox1" x:FieldModifier="private" />

Interestingly the MSDN page for x:FieldModifier doesn't give "private" as a valid value for the attribute, but I've tested it and it works.

有趣的是,x:FieldModifier的MSDN页面并没有将“private”作为属性的有效值,但是我已经对它进行了测试,它是有效的。

#1


38  

You can use the x:FieldModifier attribute on your controls, thusly:

您可以在控件上使用x:FieldModifier属性,如下所示:

<TextBox x:Name="textBox1" x:FieldModifier="private" />

Interestingly the MSDN page for x:FieldModifier doesn't give "private" as a valid value for the attribute, but I've tested it and it works.

有趣的是,x:FieldModifier的MSDN页面并没有将“private”作为属性的有效值,但是我已经对它进行了测试,它是有效的。