问题描述
我希望将组合框的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 ...
链接的对象是值,而不是属性" ...
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
绑定到ComboBox
的SelectedItem
属性:
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绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!