wpf 绑定到内部属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19704149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding to an internal property?
提问by fhnaseer
I am trying some different things using MVVM. In our ViewModel properties which are bind to View are public. I am taking example of a button binding. Here is a simple sample.
我正在尝试使用 MVVM 做一些不同的事情。在我们的 ViewModel 中,绑定到 View 的属性是公开的。我以按钮绑定为例。这是一个简单的示例。
View.xaml:
查看.xaml:
<Button Content="Test Button" Command="{Binding TestButtonCommand}" />
ViewModel.cs
视图模型.cs
private ICommand _testButtonCommand;
public ICommand TestButtonCommand
{
get { return _testButtonCommand?? (_testButtonCommand= new RelayCommand(SomeMethod)); }
}
Here my question is that can we make TestButtonCommandinternal instead of public? Internal means it is accessible to current project so their should not be any problem doing that? But when I tried to do that it didn't worked. Adding a breakpoint in getter was not hit. So why we cannot make it internal?
这里我的问题是我们可以TestButtonCommand内部而不是公开吗?内部意味着当前项目可以访问它,所以他们这样做应该没有任何问题?但是当我试图这样做时,它没有奏效。在 getter 中添加断点未命中。那么为什么我们不能让它成为内部呢?
Here is the link from msdn.
这是来自 msdn 的链接。
http://msdn.microsoft.com/en-us/library/ms743643.aspx
http://msdn.microsoft.com/en-us/library/ms743643.aspx
The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.
您用作绑定的绑定源属性的属性必须是您的类的公共属性。不能出于绑定目的访问显式定义的接口属性,也不能访问没有基本实现的受保护、私有、内部或虚拟属性。
Why we cannot do this?
为什么我们不能这样做?
In case of access internal is same as public if working in the same project. Then why we cannot use internal here? There must be a reason that these should be public, and I am looking for that reason.
如果在同一个项目中工作,则访问 internal 与 public 相同。那为什么我们这里不能使用 internal 呢?这些必须是公开的,我正在寻找这个原因。
internal ICommand TestButtonCommand { ...... }
回答by Rohit Vats
In case of access internal is same as public if working in the same project. Then why we cannot use internal here. There must be a reason that these should be public, and I am looking for that reason.
如果在同一个项目中工作,则访问 internal 与 public 相同。那么为什么我们不能在这里使用内部。这些必须是公开的,我正在寻找这个原因。
You have part of your answer in your question itself in the quote from Microsoft:
在 Microsoft 的引述中,您在问题本身中有部分答案:
The properties you use as binding source properties for a binding must be public properties of your class.
您用作绑定的绑定源属性的属性必须是您的类的公共属性。
Presumably / speculatively, the reason for thisis that internals can only be accessed within the same assembly and not from outside. Binding to internals doesn't work because binding is resolved by the WPF binding engine which is in a separate assembly PresentationFramework.dll.
推测/推测,这样做的原因是内部只能在同一个程序集中访问,而不能从外部访问。绑定到内部组件不起作用,因为绑定是由位于单独程序集中的 WPF 绑定引擎解析的PresentationFramework.dll。
回答by Nitin
Bindingis only supported for public properties. MSDN reference:
Binding仅支持公共属性。MSDN参考:
http://msdn.microsoft.com/en-us/library/ms743643.aspx
http://msdn.microsoft.com/en-us/library/ms743643.aspx
As quoted in the reference
如参考文献中所引用
The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.
您用作绑定的绑定源属性的属性必须是您的类的公共属性。不能出于绑定目的访问显式定义的接口属性,也不能访问没有基本实现的受保护、私有、内部或虚拟属性。
回答by Grim
Obviously, it depends on what you're trying to achieve from this situation - you don't state what the overall aim is. I have just come across a similar problem with my code, and also happened upon a solution for my case. One of my libraries contains helper objects with various properties, but when these are used in the application project, I only want to see the properties that are of any use to me - I wanted to hide, for example, the Command properties.
显然,这取决于您在这种情况下想要达到的目标——您没有说明总体目标是什么。我刚刚在我的代码中遇到了类似的问题,并且也遇到了针对我的案例的解决方案。我的一个库包含具有各种属性的辅助对象,但是当在应用程序项目中使用这些对象时,我只想查看对我有用的属性 - 例如,我想隐藏命令属性。
My solution to hide them from the 'user' of the library is to add the
我将它们从库的“用户”中隐藏的解决方案是添加
<EditorBrowsable(EditorBrowsableState.Never)>
attribute to each property that bears little or no interest to me.
归因于我几乎没有兴趣或没有兴趣的每个财产。
Hope that helps someone!
希望对某人有所帮助!
回答by Mike Strobel
The internalvisibility is really only meaningful to the compiler and the IL verifier, because they know the full context of the member access; the WPF binding engine does not. It knows that a binding exists on a property; it has no idea who set the property. It could have been set in the XAML, or dynamically at runtime (technically, even if you set it in the XAML, it is still applied dynamically).
在internal能见度只有真正有意义的编译器和IL验证,因为他们知道的成员访问完整的上下文; WPF 绑定引擎没有。它知道某个属性上存在绑定;它不知道是谁设置了属性。它可以在 XAML 中设置,或者在运行时动态设置(从技术上讲,即使您在 XAML 中设置它,它仍然是动态应用的)。
Since there is no way to enforce the access rules, allowing binding to internalproperties would be equivalent to allowing binding to privateproperties, not publicproperties.
由于无法强制执行访问规则,因此允许绑定到internal属性等同于允许绑定到private属性,而不是public属性。
回答by CAD bloke
From http://msdn.microsoft.com/en-us/library/ms743643.aspx
来自http://msdn.microsoft.com/en-us/library/ms743643.aspx
For CLR properties, data binding works as long as the binding engine is able to access the binding source property using reflection. Otherwise, the binding engine issues a warning that the property cannot be found and uses the fallback value or the default value, if it is available.
对于 CLR 属性,只要绑定引擎能够使用反射访问绑定源属性,数据绑定就可以工作。否则,绑定引擎会发出无法找到该属性的警告,并使用回退值或默认值(如果可用)。
回答by Aleksey
Created internal property are breaking good OO design and are breaking encapsulation. You can use internal set accessor (and public get accessor) for your case.
创建的内部属性破坏了良好的 OO 设计并破坏了封装。您可以为您的案例使用内部设置访问器(和公共获取访问器)。
public ICommand SaveCommand
{
get;
internal set;
}
If you have a field encapsulated into a property, you should make it a rule to always access that field throught a property even inside your class. It's best practise.
如果您将一个字段封装到一个属性中,您应该制定一条规则,即始终通过属性访问该字段,即使在您的类中也是如此。这是最好的做法。
回答by Vahid
It is not possible to bind to internal properties. You can make your class as internal though, if you do not want your class to be accessible outside the project.
无法绑定到内部属性。但是,如果您不希望您的课程在项目之外被访问,您可以将您的课程设置为内部课程。

