注意到我已经问了相关问题:如何将DataTrigger和EventTrigger结合起来?
我有一个清单框,里面有几个项目。项的类实现INotifyPropertyChanged
,并具有属性IsAvailable
。我使用该属性表示列表中使用不同颜色的不可用选项。
但是,如果选定的项目不可用,则前景颜色应为红色。
<ListBox>
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:InstitutionViewModel}">
<TextBlock Name="Name" Text="{Binding Name}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsAvailable}" Value="False">
<Setter TargetName="Name" Property="Foreground" Value="#888"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.Resources>
</ListBox>
我使用上述数据触发器灰色出不可用的项目。
我面临的问题是,项目被选中这一事实与模板绑定到的底层数据无关。我真正想要的是某种多触发器,它既支持依赖项上的常规Trigger
(ListBoxItem.IsSelected
),也支持绑定数据项上的DataTrigger
。
在不将选择的概念引入我的观点模型的情况下,能做到这一点吗?
对于任何想知道为什么我不禁用不可用项的人,请了解应用程序的要求是可以选择不可用选项。实际上有几个列表框,在一个效果中选择其他的效果。我不能禁用项目,因为用户将无法改变他们的想法或探索不同的组合,如果项目是禁用的基础上的早期选择。
发布于 2009-03-02 15:05:09
对于其他遇到这个问题的人,我找到了一个适合我的解决方案。当然,我仍然有兴趣看到其他有趣的答案。
我所做的是:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding
RelativeSource={
RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=IsSelected}" Value="True"/>
<Condition Binding="{Binding IsAvailable}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="Name" Property="Foreground" Value="#F00"/>
</MultiDataTrigger>
不过,这是一个多触发器,没有什么特别之处。如果您只想在数据模板中对所选项进行不同的样式设置,则可以使用:
<DataTrigger Binding="{Binding
RelativeSource={
RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},
Path=IsSelected}" Value="True">
<Setter TargetName="Name" Property="Foreground" Value="#888"/>
</DataTrigger>
发布于 2010-12-01 04:33:40
若要与DataGridRow
一起使用它,请将绑定模式更改为Self
Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=...
发布于 2021-11-16 12:15:53
对于DevExpress GridControl,有一个用于组合字段值、格式条件和行IsFocused属性的示例。
XAML:
<dxg:GridControl.View>
<dxg:TableView CustomRowAppearance="CustomRowAppearance" NavigationStyle="Row">
<dxg:TableView.FormatConditions>
<dxg:DataBarFormatCondition FieldName="Profit" PredefinedFormatName="GreenGradientDataBar" />
<dxg:FormatCondition FieldName="Profit" Expression="[Profit]<0" PredefinedFormatName="RedText"/>
<dxg:FormatCondition FieldName="Profit" Expression="[Profit]>=0" PredefinedFormatName="GreenText"/>
<dxg:FormatCondition FieldName="Profit" Expression="[Profit]<=0" PredefinedFormatName="LightRedFillWithDarkRedText" ApplyToRow="True"/>
</dxg:TableView.FormatConditions>
</dxg:TableView>
</dxg:GridControl.View>
C#:
void CustomRowAppearance(object sender, CustomRowAppearanceEventArgs e) {
if (e.RowSelectionState != SelectionState.None) {
object result = e.ConditionalValue;
if (e.Property == TextBlock.ForegroundProperty || e.Property == TextBlock.BackgroundProperty) {
SolidColorBrush original = e.OriginalValue as SolidColorBrush;
SolidColorBrush conditional = e.ConditionalValue as SolidColorBrush;
if (conditional != null && (original == null || original.Color != conditional.Color))
result = ShadeBrush(conditional);
}
e.Result = result;
e.Handled = true;
}
}
SolidColorBrush ShadeBrush(SolidColorBrush brush) {
Color originalColor = brush.Color;
float coefficient = 0.75f;
byte a = originalColor.A;
if (!grid.IsKeyboardFocusWithin) // I commented this line in WPF
a = (byte)(originalColor.A / 2);
byte r = (byte)(originalColor.R * coefficient);
byte g = (byte)(originalColor.G * coefficient);
byte b = (byte)(originalColor.B * coefficient);
return new SolidColorBrush(Color.FromArgb(a, r, g, b));
}
https://stackoverflow.com/questions/602517
复制相似问题