开发者

Why is the control inaccessible due to its protection level?

开发者 https://www.devze.com 2022-12-12 03:06 出处:网络
I\'m trying to access a control\'s text property from program.cs and it says that it 开发者_Go百科is inaccessible due to protected level. How can I fix this please?This is the default property for con

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


This is the default property for controls and can be solved by:

  1. Going into Design-View for the Form that contains the specified Control
  2. Then changing the Control's Modifiers property to Public or Internal.

Why is the control inaccessible due to its protection level?


Control Protection level Resolved

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


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.


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

0

精彩评论

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