首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WPF -如何结合DataTrigger和触发器?

WPF -如何结合DataTrigger和触发器?
EN

Stack Overflow用户
提问于 2009-03-02 14:25:47
回答 3查看 28K关注 0票数 56

注意到我已经问了相关问题:如何将DataTrigger和EventTrigger结合起来?

我有一个清单框,里面有几个项目。项的类实现INotifyPropertyChanged,并具有属性IsAvailable。我使用该属性表示列表中使用不同颜色的不可用选项。

但是,如果选定的项目不可用,则前景颜色应为红色。

代码语言:javascript
运行
复制
<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

在不将选择的概念引入我的观点模型的情况下,能做到这一点吗?

对于任何想知道为什么我不禁用不可用项的人,请了解应用程序的要求是可以选择不可用选项。实际上有几个列表框,在一个效果中选择其他的效果。我不能禁用项目,因为用户将无法改变他们的想法或探索不同的组合,如果项目是禁用的基础上的早期选择。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-03-02 15:05:09

对于其他遇到这个问题的人,我找到了一个适合我的解决方案。当然,我仍然有兴趣看到其他有趣的答案。

我所做的是:

代码语言:javascript
运行
复制
<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>

不过,这是一个多触发器,没有什么特别之处。如果您只想在数据模板中对所选项进行不同的样式设置,则可以使用:

代码语言:javascript
运行
复制
<DataTrigger Binding="{Binding 
  RelativeSource={
    RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},
    Path=IsSelected}" Value="True">
  <Setter TargetName="Name" Property="Foreground" Value="#888"/>
</DataTrigger>
票数 79
EN

Stack Overflow用户

发布于 2010-12-01 04:33:40

若要与DataGridRow一起使用它,请将绑定模式更改为Self

代码语言:javascript
运行
复制
Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=... 
票数 18
EN

Stack Overflow用户

发布于 2021-11-16 12:15:53

对于DevExpress GridControl,有一个用于组合字段值、格式条件和行IsFocused属性的示例

XAML:

代码语言:javascript
运行
复制
<dxg:GridControl.View>
    <dxg:TableView CustomRowAppearance="CustomRowAppearance" NavigationStyle="Row"> 
        <dxg:TableView.FormatConditions> 
            <dxg:DataBarFormatCondition FieldName="Profit" PredefinedFormatName="GreenGradientDataBar" /> 
            <dxg:FormatCondition FieldName="Profit" Expression="[Profit]&lt;0" PredefinedFormatName="RedText"/> 
            <dxg:FormatCondition FieldName="Profit" Expression="[Profit]&gt;=0" PredefinedFormatName="GreenText"/> 
            <dxg:FormatCondition FieldName="Profit" Expression="[Profit]&lt;=0" PredefinedFormatName="LightRedFillWithDarkRedText" ApplyToRow="True"/> 
        </dxg:TableView.FormatConditions> 
    </dxg:TableView>
</dxg:GridControl.View>

C#:

代码语言:javascript
运行
复制
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));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/602517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档