绑定到文本 [英] Binding to a Text

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

问题描述

你好

我有一个CMIcon类,其中具有如下属性:

I have a class CMIcon in which there is a property as below :

public string Text { get; set; }

此类在程序集abc.dll中,我在xaml中将其引用为以下程序:

This class is in an assembly abc.dll and i am referring this assembly as below in xaml:

xmlns:my="clr-namespace:abc.Views;assembly=abc.Core"

并按以下方式使用它:

<my:CMIconText Text="Logo" />

我想从资源文件中绑定上述文本,如下所示:

I want to bind the above Text from the resource file as below :

<my:CMIconText Text = "{Binding Path=ApplicationStrings.MyNewString, Source={StaticResource resourceLibKey}}" />

但是给我一个错误,说"System.windows.data.binding"无法转换为类型"System.string".我了解文字"这是一个字符串,因此不具有约束力.

But it is giving me an error saying that "System.windows.data.binding' cannot be converted to type 'System.string'". I understand "Text" here is a string and so it is not binding.

还有其他绑定方法吗?

谢谢

阿卜迪

推荐答案

您需要将Text属性定义为依赖项属性,以便能够将任何内容绑定为唯一的依赖项目标属性可以绑定:

You need to define the Text property as a dependency property to be able to bind anything to it as only dependency target properties can be bound:

public class CMIcon : DependencyObject
 {
  // Dependency Property
  public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string),
    typeof(CMIcon), new FrameworkPropertyMetadata(""));

  // .NET Property wrapper
  public string Text
  {
   get { return (string)GetValue(TextProperty); }
   set { SetValue(TextProperty, value); }
  }
 }


请参阅以下页面以获取有关依赖项属性的更多信息:

Please refer to the following pages for more information about dependency properties:

http://wpftutorial.net/DependencyProperties.html
http://msdn.microsoft.com/en-us/library/ms752914 (v = vs.110).aspx


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

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