绑定到内部属性? [英] Binding to an internal property?

查看:69
本文介绍了绑定到内部属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MVVM做一些不同的事情.在我们的ViewModel中,绑定到View的属性是公共的.我以按钮绑定为例.这是一个简单的示例.

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.

View.xaml:

View.xaml:

<Button Content="Test Button" Command="{Binding TestButtonCommand}" />

ViewModel.cs

ViewModel.cs

private ICommand _testButtonCommand;
public ICommand TestButtonCommand
{
    get { return _testButtonCommand?? (_testButtonCommand= new RelayCommand(SomeMethod)); }
}

这是我的问题是,我们可以将内部的TestButtonCommand设置为公开吗?内部意味着当前项目可以访问它,所以这样做应该不会有任何问题吗?但是,当我尝试这样做时,它没有用.没有在getter中添加断点.那么为什么我们不能将其设置为内部?

Here my question is that can we make TestButtonCommand internal 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?

这是msdn的链接.

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.

为什么我们不能这样做?

如果在同一项目中工作,则内部访问与公共访问相同.那为什么我们不能在这里使用内部的呢?一定有理由公开这些内容,而我正在寻找这个原因.

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 ICommand TestButtonCommand { ...... }

推荐答案

如果在同一内部工作,则内部访问与公共访问相同 项目.那为什么我们不能在这里使用内部.一定是有原因的 这些应该是公开的,我正在寻找这个原因.

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.

在Microsoft的引文中,您的问题中有部分答案:

You have part of your answer in your question itself in the quote from Microsoft:

用作绑定的绑定源属性的属性必须 是您班上的公共财产.

The properties you use as binding source properties for a binding must be public properties of your class.

大概/推测性地,这样做的原因是内部只能在同一程序集中访问,而不能从外部访问.绑定到内部组件不起作用,因为绑定是由位于单独程序集PresentationFramework.dll中的WPF绑定引擎解析的.

Presumably / speculatively, the reason for this is 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.

这篇关于绑定到内部属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆