9

ViewModel クラスを DataContext として持つ UserControl があります。

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 に設定すると、バインディングは正常に機能します。しかし、上記のようにプロパティを internal に設定すると、バインディングは失敗します (バインディング エラー: プロパティが見つかりません...)。

内部プロパティは、同じアセンブリで public のようにアクセスできると思いました。また、問題なく UserControl-codebehind からプロパティにアクセスできます。

{
...

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

...

この動作の説明はありますか?

よろしくお願いします、rhe1980

4

1 に答える 1

19

ここで述べたように

バインディングのバインディング ソース プロパティとして使用するプロパティは、クラスのパブリック プロパティである必要があります。明示的に定義されたインターフェイス プロパティには、バインディングの目的でアクセスすることはできません。また、基本実装を持たない保護されたプライベート プロパティ、内部プロパティ、または仮想プロパティにもアクセスできません。

于 2012-08-17T11:14:25.937 に答える