2011-10-06 83 views

回答

2

將ValidatesOnDataErrors和ValidatesOnExpcetions設置爲False,以便綁定您的單元格。 如果你想要你的驗證,那麼你必須爲驗證控件提供驗證模板。請參考我的答案在這裏 - Validation Error Style in WPF, similar to Silverlight

+0

我想它來驗證數據。但我想更改默認的紅色邊框樣式。 –

+0

我已經更新了我的答案。請看一看。 –

1

我討厭紅色邊框,因爲它被放置在裝飾器和裝飾器坐在窗口的頂部,這意味着如果你的元素是部分/完全由另一個元素是隱藏的(像它是在網格中)全裝修仍然顯示:(

這並不意味着我不想要一些定製,雖然,所以我仍然可以突出顯示我的項目粉紅色,或改變他們的前景,讓DataGridCell正確剪輯一切都關閉了,你可以通過使用風格觸發器來做到這一點

希望這可以幫到你

<DataGrid.Resources> 
    <Style TargetType="{x:Type TextBlock}" x:Key="TextBlockErrorStyle"> 
     <Setter Property="Validation.ErrorTemplate"> 
      <Setter.Value> 
       <ControlTemplate> 
        <!-- Just the adorned element means NO RED BORDER --> 
        <AdornedElementPlaceholder Name="controlWithError" /> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="true"> 
       <Setter Property="Foreground" Value="Red"/> 
       <Setter Property="Background" Value="Pink"/> 
       <Setter Property="ToolTip" 
        Value="{Binding RelativeSource={RelativeSource Self}, 
        Path=(Validation.Errors)[0].ErrorContent}"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.Resources> 

...

<DataGridTemplateColumn Header="Description" MinWidth="150"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Path=Description, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" 
         Style="{StaticResource ResourceKey=TextBlockErrorStyle}" 
         /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 
1

我有同樣的問題,但在我的情況我沒有使用IDataErrorINotifyDataErrorInfo。我正在使用自定義ValidationRule和樣式來處理我的驗證邏輯和演示文稿。我已經在使用DataGrid RowStyle爲有錯誤的行顯示自定義樣式,所以我認爲使用DataGridCell做類似的事情會很容易。

注意事項:

  1. 你不能只定義樣式,並設置DataGrid.CellStyle。相反,您必須使用ElementStyle和/或EditingElementStyle

  2. 樣式TargetType必須與單元格正在使用的DataGridColumn類型相匹配。所以對於一個DataGridComboBoxColumn的TargetType應該ComboBox

DataGrid列XAML:

<DataGridComboBoxColumn Header="Column1" 
    ItemsSource="{Binding Path=Column1Path}" 
    DisplayMemberPath="Column1DisplayPath" 
    ElementStyle="{StaticResource DGComboColValidationStyle}" 
    EditingElementStyle="{StaticResource DGComboColValidationStyle}"> 

    <DataGridComboBoxColumn.SelectedItemBinding> 
     <Binding Path="Column1Path" UpdateSourceTrigger="LostFocus"> 
      <Binding.ValidationRules> 
       <Validation:CustomValidationRule ValidationStep="UpdatedValue" ValidatesOnTargetUpdated="True" /> 
      </Binding.ValidationRules> 
     </Binding> 
    </DataGridComboBoxColumn.SelectedItemBinding> 
</DataGridComboBoxColumn> 

樣式定義

<Style x:Key="DGComboColValidationStyle" TargetType="{x:Type ComboBox}"> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Grid> 
        <Border BorderThickness="1" 
          BorderBrush="{Binding ElementName=adorner1, Path=DataContext[0].ErrorContent.ValidationType, Converter={StaticResource ValidationTypeColorConverter}}" 
          CornerRadius="3"> 
        </Border> 
        <AdornedElementPlaceholder Name="adorner1"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="DGTextColValidationStyle" TargetType="{x:Type TextBlock}"> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Grid> 
        <Border BorderThickness="1" 
          BorderBrush="{Binding ElementName=adorner2, Path=DataContext[0].ErrorContent.ValidationType, Converter={StaticResource ValidationTypeColorConverter}}" 
          CornerRadius="3"> 
        </Border> 
        <AdornedElementPlaceholder Name="adorner2"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="DGRowValidationStyle" TargetType="{x:Type DataGridRow}"> 
    <Setter Property="ValidationErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate x:Name="DGRowValidationTemplate"> 
       <Grid> 
        <Ellipse Width="12" Height="12" Stroke="Black" StrokeThickness="0.5" 
         Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent.ValidationType, Converter={StaticResource ValidationTypeColorConverter}}"/> 
        <TextBlock FontWeight="Bold" Padding="4,0,0,0" 
         Margin="0" VerticalAlignment="Top" Foreground="White" Text="!" 
         ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent.ValidationMessage}"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="BorderThickness" Value="2"/> 
      <Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent.ValidationType, Converter={StaticResource ValidationTypeColorConverter}}"/> 
     </Trigger> 
     <Trigger Property="Validation.HasError" Value="false"> 
      <Setter Property="BorderThickness" Value="0"/> 
      <Setter Property="BorderBrush" Value="Transparent" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

<Style x:Key="DGCellStyle" TargetType="{x:Type DataGridCell}"> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="false"> 
      <Setter Property="BorderThickness" Value="0"/> 
      <Setter Property="BorderBrush" Value="Transparent" /> 
      <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent.ValidationMessage}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

參考: http://social.msdn.microsoft.com/Forums/vstudio/en-US/6d2d6513-7bca-4359-a12b-46da3c380b0a/wpf-4-datagrid-editingelementstyle-and-validationerrortemplate-adorner-layer?forum=wpf

6

您可以在此行只是添加到您的DataGrid:

<DataGrid Validation.ErrorTemplate="{x:Null}" /> 
相關問題