1
votes

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

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

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?

Thanks!

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

EDIT :

my classes :

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 linked to an ObservableCollection<Attribut>()

1
It fails because your data context is not right. Can you show how the data context is provided for your ComboBox (and parent objects)?James Harcourt
@JamesHarcourt I edited the post with the description of the classesWDKyle
@WDKyle: Why isn't the ALV_COULEUR property defined in the Attribut class? Or do you want a different colour for each item in the ComboBox?mm8
@mm8: Yes, for each item.WDKyle

1 Answers

1
votes

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>