我在WPF项目中使用MahApps
,并且我尝试使用MahApps
的内置样式在DataGrid
控件上提供一些验证,但我定义的验证方式没有显示。
DataGrid
是:
<DataGrid BorderThickness="2" Margin="0,10,0,93" AutoGenerateColumns="False" FontSize="18"
SelectionUnit="FullRow" SelectionMode="Single"
ItemsSource="{Binding DocumentLines, Mode=TwoWay}" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding ProductName}" Width="*" IsReadOnly="True"/>
<DataGridTextColumn Header="Code" Binding="{Binding ProductCode}" Width="*" IsReadOnly="True"/>
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity, ValidatesOnExceptions=True}" Width="*"/>
<DataGridTemplateColumn Header="VATPercentage" Width="*" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding VATPercentage, StringFormat=N}" TextAlignment="Right" Margin="0,0,5,0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.RowValidationRules>
<val:Validation ValidationStep="UpdatedValue"/>
</DataGrid.RowValidationRules>
<DataGrid.RowValidationErrorTemplate>
<ControlTemplate>
<Grid Margin="0,-2,0,-2"
ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow},
Path=(Validation.Errors)[0].ErrorContent}">
<Path Height="20" Width="20" Stretch="Uniform" Fill="Red" Data="F1 M 12.2495,36.8804L 36.8804,12.2496C 37.4987,11.6312 38.5012,11.6312 39.1196,12.2496L 63.7504,36.8804C 64.3688,37.4988 64.3688,38.5013 63.7504,39.1196L 39.1196,63.7505C 38.5013,64.3688 37.4987,64.3688 36.8804,63.7505L 12.2495,39.1196C 11.6312,38.5013 11.6312,37.4988 12.2495,36.8804 Z M 38,45.125C 36.0325,45.125 34.4375,46.72 34.4375,48.6875C 34.4375,50.655 36.0325,52.25 38,52.25C 39.9675,52.25 41.5625,50.655 41.5625,48.6875C 41.5625,46.72 39.9675,45.125 38,45.125 Z M 34.4375,23.75L 35.625,42.75L 40.375,42.75L 41.5625,23.75L 34.4375,23.75 Z"/>
</Grid>
</ControlTemplate>
</DataGrid.RowValidationErrorTemplate>
</DataGrid>
ValidationRule
是:
public class Validation : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
Line line = (value as BindingGroup).Items[0] as Line;
if (line.Quantity <= 0)
return new ValidationResult(false, "Erro");
return ValidationResult.ValidResult;
}
}
我在Onedrive sample发布了一个小样本 只有在App.xaml上未引用MahApps样式时才会显示验证样式。
任何人都可以帮我吗?