【问题标题】:Xaml converter in c# codeC# 代码中的 Xaml 转换器
【发布时间】:2013-06-29 21:33:59
【问题描述】:

我有这个代码:

Color color = GetMyColor();
string s = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><StackPanel Background=""" + color + "\" ><TextBlock Text=\"{Binding Label}\"  HorizontalAlignment=\"Center\"   /></StackPanel></DataTemplate>";
var MyObject.Template = (DataTemplate)XamlReader.Load(s);

我想在我的绑定中添加一个转换器,如下所示:

Color color = GetMyColor();
string s = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><StackPanel Background=""" + color + "\" ><TextBlock Text=\"{Binding Converter={StaticResource NumberConverter}}\"  HorizontalAlignment=\"Center\"   /></StackPanel></DataTemplate>";
var MyObject.Template = (DataTemplate)XamlReader.Load(s);

但是,这不起作用,因为我需要添加我认为的转换器的命名空间。

我该怎么做?

谢谢

【问题讨论】:

    标签: silverlight xaml c#-4.0 converter


    【解决方案1】:

    阿法伊克, StaticResource 仅按名称工作。它看起来资源。好像不是命名空间的问题。

    您可以通过

    轻松添加您的命名空间
        @"<DataTemplate xmlns:local=""clr-namespace:YourProjectBus.Converters"" 
    

    除此之外,请确保您的静态资源 (NumberConverter) 之前添加到运行中 班级。 必须在之前完成

     var MyObject.Template = (DataTemplate)XamlReader.Load(s);
    

      ctor()
    {
       this.Resources.Add("NumberConverter",new NumberConverter());
    }
    

    检查输出,输出窗格中显示的绑定错误。 祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      相关资源
      最近更新 更多