I'm using MahApps
on a WPF project, and I'm trying to provide some validation on a DataGrid
control, using the built in style of MahApps
, but the validation style I've defined is not showing.
The DataGrid
is:
<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>
The ValidationRule
is:
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;
}
}
I've posted a small working sample in Onedrive sample The validation style appears only when the MahApps style are not referenced on App.xaml.
Can anyone help me with this?
Apparently this as been solved for version 1.2 of Mahapps.Metro, see here for more details: