Binding to internal ViewModel-Property

若如初见. 提交于 2019-12-05 19:45:34

问题


I have a UserControl with a ViewModel class as 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>

CodeBehind:

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

ViewModel class:

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

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

If I set the ViewModelProperty to public the binding works fine. But if I set the property to internal like above the binding fails (Binding error: property not found... ).

I thought an internal property is accessible like public in same assembly. Also I can access to the property from UserControl-codebehind without any problem:

{
...

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

...

Any explenation for this behavior?

Thanks in advance, rhe1980


回答1:


As stated here

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.



来源:https://stackoverflow.com/questions/12004642/binding-to-internal-viewmodel-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!