本文介绍了绑定到内部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

推荐答案

如所述

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

05-26 22:16
查看更多