【问题标题】:c# class for constants bindable in xaml as well as in codec# 用于可在 xaml 和代码中绑定的常量的类
【发布时间】:2013-07-09 15:05:53
【问题描述】:

我想要一个带有常量字符串的类,例如herehere。但我希望能够在 xaml 和代码中绑定到这些值,例如:

<Window x:Class="Ccompany.Product.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:lex="http://wpflocalizeextension.codeplex.com"
        lex:ResxLocalizationProvider.DefaultAssembly="<-- would like to be able to insert the constant here -->"                
        >

您能否也提供一个示例,说明如何在 xaml 中绑定建议的“常量类”解决方案?

注意:我在发布之前尝试了不同的方法,尝试将常量包装到属性中,但我仍然无法在 xaml 中绑定它。

【问题讨论】:

  • I tried different things before posting 准确显示您尝试了什么以及为什么它不起作用。

标签: c# xaml


【解决方案1】:

为什么不只是这个?:

{x:Static ns:TypeName.ConstValue}

这适用于如下常量值:

public class TypeName
{
    public const int ConstValue = 5;
}

【讨论】:

  • Constants.cs:namespace Company.Product.Common.Constants { public class Constants2 { public const string DefaultAssembly = "value"; } }。 MainWindow.xaml:&lt;Window ... xmlns:constants="clr-namespace:Company.Product.Common.Constants" lex:ResxLocalizationProvider.DefaultAssembly="{x:Static constants:Constants2.DefaultAssembly}" ...&gt; 产生:名称“Constants2”在命名空间“clr-namespace:Company.Product.Common.Constants”中不存在。
  • 今天我创建了一个空(新)项目,尝试了您的解决方案并且它有效。我之前尝试过的项目一定有问题。我记得我试图清理/重建甚至重新启动 Visual Studio - 没有成功。我想为我的错误道歉。您的答案实际上是正确的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多