开发者

Is it possible to prevent a XAML element with an x:Name from being accessible outside of the class in which it is defined?

开发者 https://www.devze.com 2023-03-26 10:40 出处:网络
Say I have a control with a label, and I want to be able to reference that label from code-behind. I can accomplish this by giving the label an x:Name:

Say I have a control with a label, and I want to be able to reference that label from code-behind. I can accomplish this by giving the label an x:Name:

<UserControl x:Class=...>
    <Label x:Name="someLabel">Foo</Label>
    ....
</UserControl>

Unfortunately, now I can do this in a different class:

`someUserControl.someLabel.Content = ...`

Is there any way to limit access to the Label to the class in which it is defined? I.e., I would still be able to reference开发者_运维知识库 it in the UserControl's code-behind, but nowhere else.


By default they will be internal, and the documentation says you can use the x:FieldModifier attribute to make it public. Regardless of what the docs say, you can do x:FieldModifier="private" for C# to make the field private.

0

精彩评论

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