我有一个带有ViewModel类的UserControl作为DataContext:

XAML

<UserControl x:Class="DotfuscatorTest.UserControl.View.UserControlView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d" >
<StackPanel>
  <TextBox Text="{Binding ViewModelProperty}"/>
</StackPanel>
</UserControl>

代码背后:
namespace DotfuscatorTest.UserControl.View
{
   using ViewModel;
   public partial class UserControlView
   {
      public UserControlView()
      {
         InitializeComponent();
         DataContext = new UserControlViewModel();
      }
   }
}

ViewModel类:
namespace DotfuscatorTest.UserControl.ViewModel
{
   public class UserControlViewModel
   {
      private string viewModelProperty = "hello world";

      internal string ViewModelProperty
      {
        get { return viewModelProperty; }
        set { viewModelProperty = value; }
      }
   }
}

如果我将ViewModelProperty设置为public,则绑定(bind)可以正常工作。但是如果我像上面那样将属性设置为internal,绑定(bind)将失败(绑定(bind)错误:找不到属性...)。

我以为在同一程序集中可以像访问公共(public)属性那样访问内部属性。我也可以从UserControl-codebehind访问该属性,而不会出现任何问题:
{
...

((UserControlViewModel)DataContext).ViewModelProperty = "hallo viewmodel";

...

这种行为有什么生气吗?

提前致谢,
rhe1980

最佳答案

如所述here

关于c# - 绑定(bind)到内部ViewModel-Property,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12004642/

10-12 12:49
查看更多