【发布时间】:2014-08-26 21:18:54
【问题描述】:
我无法使组合框项目仅在它是选定项目并且其基础 ID 与 ItemSource 中的最后一个 ID 匹配时以红色字体显示。这是我到目前为止所得到的,只有当它是选定的项目时它才会变成红色,但是我如何检查底层属性“ID”是否与包含它的 ObservableCollection 中的最后一个条目匹配,即仅当SelectedItem 的 ID== Collection[Length-1].ID?
<ComboBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
ItemsSource="{Binding BillingCycles}" SelectedItem="{Binding SelectedBillingCycle}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding BillingCycleDescription}" >
<TextBlock.Foreground>
<MultiBinding Converter="{StaticResource IsCurrentCycleColorConverter}">
<Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType={x:Type ComboBox}}"/>
<Binding Path="SelectedItem" RelativeSource="{RelativeSource AncestorType={x:Type ComboBox}}"/>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type ComboBoxItem}}"/>
</MultiBinding>
</TextBlock.Foreground>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
因此,如果 SelectedBillingCycle.ID != BillingCycles[BillingCycle.Length-1].ID,文本应显示为红色。我不确定如何引用 BillingCycles[BillingCycles.Length-1] 来比较它。
///编辑:
更改了 XAML,这越来越接近,但更改了所有组合框项,而不仅仅是选定项。我想我需要使用某种模板选择器或完全重新考虑 XAML。
【问题讨论】:
-
看起来您必须使用
Converter将ID转换为true(通过与BillingCycles[BillingCycle.Length-1].ID进行比较(即Converter用于Condition中的绑定)。 -
好的,但是我如何将 SelectedItem 的 ID AND 和 BillingCycles[BillingCycles.Length-1] 的 ID 指定给转换器?
-
也许我需要两个模板?因为我只希望 SelectedItem 变成红色,而不是组合框中的其他项目?
标签: c# wpf xaml combobox itemtemplate