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


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

查看所有标签

猜你喜欢:

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

Linux/UNIX系统编程手册

Linux/UNIX系统编程手册

Michael Kerrisk / 孙剑 许从年 董健、孙余强 郭光伟 陈舸 / 人民邮电出版社 / 2014-1 / 158

《linux/unix系统编程手册(上、下册)》是介绍linux与unix编程接口的权威著作。linux编程资深专家michael kerrisk在书中详细描述了linux/unix系统编程所涉及的系统调用和库函数,并辅之以全面而清晰的代码示例。《linux/unix系统编程手册(上、下册)》涵盖了逾500个系统调用及库函数,并给出逾200个程序示例,另含88张表格和115幅示意图。 《li......一起来看看 《Linux/UNIX系统编程手册》 这本书的介绍吧!

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

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具