Why is the control inaccessible due to its protect

2019-01-15 12:45发布

I'm trying to access a control's text property from program.cs and it says that it is inaccessible due to protected level. How can I fix this please?

4条回答
Fickle 薄情
2楼-- · 2019-01-15 13:09

The concept behind is the protection level. As we have studied in Object Oriented Paradigm keep your class members variables private and set or get it from Property.Thats why it is not a good approach

查看更多
smile是对你的礼貌
3楼-- · 2019-01-15 13:13

Use x:FieldModifier="public" e.g.

<TextBlock x:FieldModifier="public" x:Name="AccessibleTextBlock" />

as explained here: Modifying XAML named field visibility

In my case, I put UserControl in another DLL. WPF’s convention is to set all named fields as internal by default. Using the x:FieldModifier="public" has solved the issue.

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-15 13:20

This is the default property for controls, and can be solved by going into Design-View for the Form that contains the specified Control, then changing the Control's Modifiers property to Public or Internal. That oughta do it! :)

查看更多
ら.Afraid
5楼-- · 2019-01-15 13:22

Control Protection level Resolved

Go to designer file search control By ID e.g txtModel change protected modifier to public modifier

查看更多
登录 后发表回答