2012-08-17 23 views
9

Ho un UserControl con una classe ViewModel come DataContext:Binding per interno ViewModel-Proprietà

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 classe:

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

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

Se imposto il ViewMod elProperty to public the binding funziona bene. Ma se imposto la proprietà su internal come sopra il binding fallisce (errore di binding: property not found ...).

Ho pensato che una proprietà interna sia accessibile come pubblica nello stesso assembly. Inoltre posso accedere alla proprietà da UserControl-codebehind senza alcun problema:

{ 
... 

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

... 

Qualsiasi explenation per questo comportamento?

Grazie in anticipo, rhe1980

+0

Avresti dovuto uno sguardo su documenti DataBinding prima. – HichemSeeSharp

risposta

19

Come affermato 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.

+0

grazie per la tua risposta! Ho pensato che il punto di vista della proprietà è lo stesso se la classe è pubblica e la proprietà è interna come quando la classe è interna e la proprietà pubblica. Ma sembra che il collegamento non: -) ... – rhe1980