Mindscape WPF Elements使用教程:错误管理

原文转载自慧都控件网:http://www.evget.com/zh-CN/Info/catalog/18232.html

提高错误管理

Mindscape WPF Elements中现在又优化了错误管理,可以通过定制属性网格来提供更多的信息的错误处理。

修改自定义编辑器

Mindscape WPF Elements中提供了一个自定义编辑属性,你可以创建你的错误管理的用户界面来作为你模版的一部分,下面的代码显示“英尺和尺寸”的编辑器使用tooltip和弹出窗口用来显示验证错误。

附加验证的编辑器

<DataTemplate x:Key='FeetAndInchesEditorWithValidation'>
    <ms:TextBox BorderThickness='0' Text='{Binding Value, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource FeetAndInchesConverter}, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}' Style='{StaticResource TextBoxStandardErrorHandling}'>
    </ms:TextBox>
  </DataTemplate>

常见样式的文本处理验证错误

<Style TargetType='{x:Type ms:TextBox}' x:Key='TextBoxStandardErrorHandling'>
    <Setter Property='Control.Template'>
      <Setter.Value>
        <ControlTemplate TargetType='{x:Type ms:TextBox}'>
          <DockPanel>
            <ToggleButton x:Name='ErrorPlaceholder' Style='{StaticResource ErrorPopupTriggerStyle}'/>
            <Popup PlacementTarget='{Binding ElementName=ErrorPlaceholder}' AllowsTransparency='True' StaysOpen='False' Placement='Bottom' IsOpen='{Binding ElementName=ErrorPlaceholder, Path=IsChecked}'>
              <ItemsControl ItemTemplate='{StaticResource ValidationErrorMessage}' ItemsSource='{Binding RelativeSource={RelativeSource AncestorType={x:Type ms:TextBox}}, Path=(Validation.Errors)}'/>
            </Popup>
            <ScrollViewer x:Name='PART_ContentHost'/>
          </DockPanel>
          <ControlTemplate.Triggers>
            <Trigger Property='Validation.HasError' Value='True'>
              <Setter TargetName='ErrorPlaceholder' Property='Visibility' Value='Visible'/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    <Setter Property='Validation.ErrorTemplate'>
      <Setter.Value>
        <ControlTemplate>
          <DockPanel>
            <Ellipse Margin='0,0,6,0' ToolTip='{Binding ElementName=EditorHolder, Path=AdornedElement.(Validation.Errors)[0], Converter={StaticResource InnerExceptionExtractor}}' Width='8' Height='8' Fill='Red'/>
            <AdornedElementPlaceholder x:Name='EditorHolder'/>
          </DockPanel>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

内置的编辑器样式

对于内置编辑器的属性,可以使用样式通过属性编辑器来创建你的错误处理的用户界面。

还可以定义错误处理用户接口,通过内置编辑器的所有实例来应用,而不是在属性和属性的基础上,使用BuiltInEditorStyles属性。这个例子展示了如何添加错误处理到文本编辑器,值得注意FirstName和Surname着两种属性会自动的得到新的样式,尽管没有自定义风格的属性编辑器分配给这两个属性。

添加错误处理到内置文本编辑器

<ms:PropertyGrid.BuiltInEditorStyles>
        <ms:BuiltInEditorStyleCollection>
          <ms:BuiltInEditorStyle EditorKey='{x:Static ms:PropertyGrid.SimpleTextEditorKey}' Style='{StaticResource TextBoxStandardErrorHandling}'/>
        </ms:BuiltInEditorStyleCollection>
      </ms:PropertyGrid.BuiltInEditorStyles>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章