C#WPF组合框TextElement.Foreground绑定 [英] C# WPF Combobox TextElement.Foreground Binding

查看:568
本文介绍了C#WPF组合框TextElement.Foreground绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将组合框的TextElement.Foreground属性链接到对象的"ALV_COULEUR"变量:"tValeur".

I wish to link the TextElement.Foreground property of my combo box to the variable : "ALV_COULEUR" of my object : "tValeur".

我在输出中注意到它找不到变量ALV_COULEUR ...

I note in output that it does not find variable ALV_COULEUR ...

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

System.Windows.Data Error: 40 : BindingExpression path error: 'ALV_COULEUR' property not found on 'object' ''Attribut' (HashCode=35307513)'. BindingExpression:Path=ALV_COULEUR; DataItem='Attribut' (HashCode=35307513); target element is 'ComboBox' (Name=''); target property is 'Foreground' (type 'Brush')

链接的对象是值,而不是属性" ...

The linked object is the value and not the 'Attribut' ...

在这种情况下无法进行绑定?

It is not possible to make a binding in this case?

谢谢!

<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; }
}

DataContext:DataGrid链接到ObservableCollection<Attribut>()

DataContext : DataGrid linked to an ObservableCollection<Attribut>()

推荐答案

TextBlock定义ItemTemplate并将其Foreground属性绑定到您的ALV_COULEUR源属性.还要将T extBlock.Foreground绑定到ComboBoxSelectedItem属性:

Define an ItemTemplate with a TextBlock and bind its Foreground property to your ALV_COULEUR source property. Also bind the TextBlock.Foreground to the SelectedItem property of the ComboBox:

<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>

这篇关于C#WPF组合框TextElement.Foreground绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆