【问题标题】:wpf combobox trigger for Validation.HasErrorValidation.HasError 的 wpf 组合框触发器
【发布时间】:2012-05-07 16:35:08
【问题描述】:

我环顾四周,但无法具体找到我的问题。我知道 WPF 中的默认“错误”处理会在控件周围放置一个“装饰器”,以防基于 IDataErrorInfo 或 Validataion 规则导致给定控件失败的任何错误。这一切都很好,但是,使用选项卡式页面界面,如果任何控件被标记为无效,它们会被适当地装饰成红色边框。但是,一旦您从标签页 1 转到 2 并返回到 1,所有的装饰器都消失了(坏的)。这已经被问过,并且解决方案被接受,但正在寻找更好的替代方案。

所以,我去了我的“主题”声明,对于文本框控件,我只是说将控件的整个背景颜色设置为红色,而不仅仅是边框。没有通过 Notify on Property Changed 进行任何花哨的强制触发,如果我在页面之间切换,整个文本框的红色背景保持不变。

现在,进入组合框控件。对于那些自定义了自己的控件,甚至查看了默认的 MS 版本控件的人来说,它实际上是一堆控件、网格、列、按钮等的集合,以使组合框的魔力发挥作用。简而言之……

控制模板 网格(两列,一列用于选择的文本显示,第二列用于下拉箭头) 跨越两列的边框 路径(组合框下拉图像的线条图/字形)

ControlTemplate TargetType 文本框(作为整个组合框集的一部分) 边框特别是“PART_ContentHost”

组合框的ControlTemplate 网格 切换按钮 下拉显示列表 其他触发器..

最后,由以上组件模板化的主 ComboBox 声明。

无论如何,我这辈子都得不到这个。在组合框声明的“切换按钮”区域中,我有一个触发器将背景更改为 OBVIOUS 关闭颜色,以证明在 ControlTemplate 声明中的正确位置测试触发器工作。

所以,知道这是组合框声明中的正确位置,如果数据有错误,我想用红色代替绿色背景颜色。我知道整体“Validation.HasError”被正确触发,如本机错误处理程序所示。无论模板中如何/在何处尝试将背景颜色更改为红色,它都不起作用。我什至尝试过 DataTriggers,使用转换器,尝试多个属性,但似乎不合作。

有什么建议吗?这真的很烦人。

【问题讨论】:

    标签: wpf validation c#-4.0 datatrigger


    【解决方案1】:

    终于,明白了……并没有我想象的那么明显。无论如何,这就是我发现的。如果您使用 Microsoft 组合框模板中的示例,他们首先提供整体的两列“ToggleButton”声明

    <ControlTemplate TargetType="ToggleButton" 
       x:Key="baseComboBoxToggleButton" >
    
      ... blah blah...
    
    </ControlTemplate>
    

    然后,声明组合框的“显示值”

    <ControlTemplate TargetType="TextBox" x:Key="ComboBoxTextBox" >
       <Border x:Name="PART_ContentHost" Focusable="False" 
               Background="{TemplateBinding Background}" />
    </ControlTemplate>
    

    然后,将它们捆绑在一起作为一个 Combobox“包装器”声明

    <ControlTemplate TargetType="ComboBox" x:Key="ComboBoxGridControlTemplate" >
       <Grid x:Name="GridComboWrapper">
          <!-- This is the dropdown button that POINTS TO THE "baseComboBoxToggleButton at the top -->
          <ToggleButton Name="ToggleButton" 
            Template="{StaticResource baseComboBoxToggleButton}" 
            Grid.Column="2" Focusable="false"
            IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay,
                    RelativeSource={RelativeSource TemplatedParent}}"
                    ClickMode="Press" >
          </ToggleButton>
    
          ...
          rest of the content presenter,
          EDIT(able) textbox area,
          popup area of combobox when in drop-down mode
    
       </Grid> 
    
    
       <ControlTemplate.Triggers>
    
          <!-- PUT THE VALIDATION CHECK HERE -->
          <Trigger Property="Validation.HasError" Value="true">
             <Setter Property="ToolTip" 
                     Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                     Path=(Validation.Errors)[0].ErrorContent}"/>
    
    
             <!-- THIS IS THE CRITICAL COMPONENT... I HAD TO EXPLICITLY TELL
                  The TagetName as the "ToggleButton" and change ITs Background property
                  and it now works -->
             <Setter TargetName="ToggleButton" Property="Background" 
                     Value="{StaticResource BrushDataInvalidBorder}" />
          </Trigger>
    
       </ControlTemplate.Triggers>
    </ControlTemplate>
    

    所以,现在它可以按预期工作,并且不会因为给定表单上的活动页面更改并清除它而丢失任何装饰器......它按预期对每个单独的控件都是静态的......哇...... PITA 这个是。

    希望它可以帮助其他人在学习这种嵌套级别的内容时避免过度头撞墙。

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 1970-01-01
      • 2012-05-28
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多