绑定到内部ViewModel-Property [英] Binding to internal ViewModel-Property

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

问题描述

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

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:

CodeBehind:

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

ViewModel类:

ViewModel class:

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

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

如果我设置了ViewModelProperty公开装订作品很好。但是,如果我将属性设置为上述内部属性,则绑定失败(绑定错误:找不到属性...)。

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... ).

我认为内部属性可以像public in相同的程序集。我也可以从UserControl-codebehind后面访问该属性,而不会出现任何问题:

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";

...

对此行为有何表彰?

在此先感谢
rhe1980

Thanks in advance, rhe1980

推荐答案

如所述此处


用作绑定的绑定源属性的属性必须
是类的公共属性。明确定义的接口
属性不能出于绑定目的而访问,也不能保护没有基础
实现的
私有,内部或虚拟属性。

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.

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

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