C#WPF组合框TextElement.前台绑定

C#WPF组合框TextElement.前台绑定,c#,wpf,xaml,combobox,binding,C#,Wpf,Xaml,Combobox,Binding,我希望将组合框的TextElement.Foreground属性链接到我的对象的变量:“ALV\u COULEUR”:“tValeur” 我在输出中注意到它没有找到变量ALV_COULEUR System.Windows.Data错误:40:BindingExpression路径错误: 在“对象”“属性”上未找到“ALV_COULEUR”属性 (HashCode=35307513)”。BindingExpression:Path=ALV_COULEUR; DataItem='attribute'

我希望将组合框的
TextElement.Foreground
属性链接到我的对象的变量:
“ALV\u COULEUR”
“tValeur”

我在输出中注意到它没有找到变量ALV_COULEUR

System.Windows.Data错误:40:BindingExpression路径错误: 在“对象”“属性”上未找到“ALV_COULEUR”属性 (HashCode=35307513)”。BindingExpression:Path=ALV_COULEUR; DataItem='attribute'(HashCode=35307513);目标元素是“ComboBox” (名称=“”);目标属性为“前景”(类型为“笔刷”)

链接对象是值而不是“属性”

在这种情况下不可能进行绑定

谢谢

<ComboBox IsEditable="True"
          TextElement.Foreground="{Binding ALV_COULEUR, Converter={StaticResource IntToBrushConverter}, Mode=OneWay}"
          ItemsSource="{Binding tValeur, Mode=OneWay}" SelectedValuePath="ALV_ID" DisplayMemberPath="ALV_VALEUR"
          SelectedValue="{Binding ATT_VALEUR, Converter={StaticResource StringToIntConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          IsEnabled="{Binding IsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
</ComboBox>

编辑:

我的班级:

public class Attribut
{
    public int                      ATT_ID          { get; set; }
    public string                   ATT_LIBELLE     { get; set; }

    public List<ValeurAttribut>     tValeur         { get; set; }
}

public class ValeurAttribut
{
    public int      ALV_ID      { get; set; }
    public string   ALV_VALEUR  { get; set; }
    public int      ALV_COULEUR { get; set; }
}
公共类属性
{
公共int ATT_ID{get;set;}
公共字符串ATT_LIBELLE{get;set;}
公共列表tValeur{get;set;}
}
公共类valeuratibut
{
public int ALV_ID{get;set;}
公共字符串ALV_VALEUR{get;set;}
公共int ALV_COULEUR{get;set;}
}

DataContext:
DataGrid
链接到一个
ObservableCollection()

使用
TextBlock
定义一个
ItemTemplate
,并将其
前台
属性绑定到您的
ALV_COULEUR
源属性。还将T
extBlock.Foreground
绑定到组合框的
SelectedItem
属性:

<ComboBox IsEditable="True"
          TextBlock.Foreground="{Binding SelectedItem.ALV_COULEUR, Converter={StaticResource IntToBrushConverter}, RelativeSource={RelativeSource Self}}">
          ItemsSource="{Binding tValeur, Mode=OneWay}" SelectedValuePath="ALV_ID" DisplayMemberPath="ALV_VALEUR"
          SelectedValue="{Binding ATT_VALEUR, Converter={StaticResource StringToIntConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          IsEnabled="{Binding IsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ALV_VALEUR}" Foreground="{Binding ALV_COULEUR, Converter={StaticResource IntToBrushConverter}}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

ItemsSource=“{Binding tValeur,Mode=OneWay}”SelectedValuePath=“ALV_ID”DisplayMemberPath=“ALV_VALEUR”
SelectedValue=“{Binding ATT_VALEUR,Converter={StaticResource StringToIntConverter},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}”
IsEnabled=“{Binding IsEnabled,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}>

它失败,因为您的数据上下文不正确。你能展示一下如何为你的组合框(和父对象)提供数据上下文吗?@JamesHarcourt我编辑了这篇文章,其中描述了classes@WDKyle:为什么没有在Attribute类中定义ALV_COULEUR属性?或者您希望组合框中的每个项目都有不同的颜色吗?@mm8:是的,每个项目都有不同的颜色。我已经测试过了,但它不起作用,因为我的组合框是可编辑的。所以如果我放一个文本块,我什么都抓不到。我不懂。你可以复制所选项目的文本,是吗?对于可编辑的组合框,这种方法应该可以很好地工作。你试过了吗?是的,我说得不好。它适用于可选项目,但对于文本框中的选定项目,它保持黑色。使用TextElement。前景所有颜色都很好。很好!那么,是否有义务对所选项目和其他项目分别进行管理?谢谢:)我刚刚意识到,当输入文本时,颜色会变为黑色:(