Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight Datatemplate,后面有转换器代码_C#_Silverlight_Code Behind - Fatal编程技术网

C# Silverlight Datatemplate,后面有转换器代码

C# Silverlight Datatemplate,后面有转换器代码,c#,silverlight,code-behind,C#,Silverlight,Code Behind,我的combobox没有XAML,我想在代码中添加一个带有值转换器的datatemplate,并将其附加到combobox。这是我现在的代码,它不起作用。它说找不到我的staticresource SelectableColorConverter this.Resources.Add("SelectableColorConverter", new SelectableColorConverter()); string template = "<DataTem

我的combobox没有XAML,我想在代码中添加一个带有值转换器的datatemplate,并将其附加到combobox。这是我现在的代码,它不起作用。它说找不到我的staticresource SelectableColorConverter

        this.Resources.Add("SelectableColorConverter", new SelectableColorConverter());
        string template = "<DataTemplate xmlns=\"http://schemas.microsoft.com/client/2007\"><TextBlock Text=\"{Binding}\" Foreground=\"{Binding Converter={StaticResource SelectableColorConverter}}\" /></DataTemplate>";
        DataTemplate dt = XamlReader.Load(template) as DataTemplate;
        this.ItemTemplate = dt;
    }
this.Resources.Add(“SelectableColorConverter”,new SelectableColorConverter());
字符串模板=”;
DataTemplate dt=XamlReader.Load(template)作为DataTemplate;
this.ItemTemplate=dt;
}

任何帮助都将不胜感激。SelectableColorConverter是一个IValueConverter

您需要将指向静态资源(SelectableColorConverter)的链接添加到您的数据模板中,如下所示:

string template = "<DataTemplate xmlns=\"http://schemas.microsoft.com/client/2007\"";
templete += "xmlns:local=\"clr-namespace:NamespadeName; assembly=AssemblyName\">"
template += "<DataTemplate.Resources> <local:SelectableColorConverter x:Key=\"colorConverter\"/>";
template += "</DataTemplate.Resources>";
template += "<TextBlock Text=\"{Binding}\" Foreground=\"{Binding ., Converter={StaticResource ResourceKey=colorConverter}}\" />";
template += "</DataTemplate>";
string template=“”
模板+=“”;
模板+=“”;
模板+=“”;
模板+=“”;
将SelectableColorConverter添加到App.xaml中的资源中:

<Application
x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:convertor="using:Test.Converters"
xmlns:local="using:Test">

<Application.Resources>
    <convertor:SelectableColorConverter x:Key="SelectableColorConverter"/>
</Application.Resources>
</Application>

string template = "<DataTemplate xmlns=\"http://schemas.microsoft.com/client/2007\">";
template += "<TextBlock Text=\"{Binding}\" Foreground=\"{Binding ., Converter={StaticResource SelectableColorConverter}}\" />";
template += "</DataTemplate>";
字符串模板=”;
模板+=“”;
模板+=“”;

您想完成什么?最后一步是让组合框中的项目根据转换器中的规则更改颜色。combobox不能使用comboboxitems,因此不能一次对每个项目应用模板。我相信应用一个项目模板是实现这一点的唯一方法,但似乎你不能严格地从代码隐藏中附加一个带有转换器的模板。不同的项目有不同的颜色吗?或者一次是否只有一种颜色应用于所有项目,以便所有项目的颜色一致?