c# – XAML使用参数绑定到静态方法

栏目: ASP.NET · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/15520579/xaml-bind-to-static-method-with-parameters

我有一个静态类,如下所示:

public static class Lang
{
   public static string GetString(string name)
   {
      //CODE
   }
}

现在我想在xaml中访问这个静态函数作为绑定.

有没有这样一个例子:

<Label Content="{Binding Path="{x:static lang:Lang.GetString, Parameters={parameter1}}"/>

还是为每个可能的参数创建一个ObjectDataProvider?

希望有人能够帮助我.提前致谢!

我也得到这个需要.我使用转换器“解决了”(如建议 here ).

首先,创建一个返回转换字符串的转换器:

public class LanguageConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    if (parameter == null)
      return string.Empty;

    if (parameter is string)
      return Resources.ResourceManager.GetString((string)parameter);
    else
      return string.Empty;
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

然后将其用于XAML:

<Window.Resources>
  <local:LanguageConverter x:Key="LangConverter" />
</Window.Resources>

<Label Content="{Binding Converter={StaticResource LangConverter}, 
                         ConverterParameter=ResourceKey}"/>

问候.

http://stackoverflow.com/questions/15520579/xaml-bind-to-static-method-with-parameters


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

风向

风向

何宝宏 / 人民邮电出版社 / 2019-1 / ¥68.00元

★这是处于不断变化的互联网时代,行业从业者与非专业从业者都应阅读的解惑之书。 ★揭示互联网思想和精神的“内核”,帮助更多人了解互联网基因。 ★看清人工智能、区块链、大数据、云计算等技术发展的规律和机会。 ★为投资者、创业者提供方向,为广大技术从业者了解技术,为就业择业者提供建议和参考。 ★中国信通院院长刘多、腾讯云总裁邱跃鹏做序推荐。 ★中国工程院院士邬贺铨、中国科学......一起来看看 《风向》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器