防止在Error上更新为ObservableCollection

时间:2015-01-15 20:16:35

标签: wpf xaml

当xaml中存在验证错误时,有没有办法阻止更新到ObservableCollection列表?我在xaml中使用“Binding”

我的xaml

    

<ControlTemplate x:Key="ErrorTemplate">
    <DockPanel LastChildFill="True">
        <Border BorderBrush="Red" BorderThickness="1">
            <AdornedElementPlaceholder />
        </Border>
    </DockPanel>
</ControlTemplate>

<Style TargetType="TextBlock">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
  Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

</Window.Resources>
<DataGrid Name="grid" HorizontalAlignment="Stretch" ItemsSource="{Binding mMngModelList}"   Margin="0,0,0,50" VerticalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="False">

    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Name">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Name}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>                
        <DataGridTemplateColumn Header="Type">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Type}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Type}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Range Left">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding RangeLeft,ValidatesOnDataErrors=True, NotifyOnValidationError=True, ValidatesOnExceptions=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding RangeLeft, ValidatesOnDataErrors=True, NotifyOnValidationError=True, ValidatesOnExceptions=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

背后的代码是

public string this[string columnName]
{
get
{
    var result = string.Empty;
    switch (columnName)
    {
        case "RangeLeft":
            if (RangeLeft == 0)
            {
                result = "RangeLeft should be greater than zero";
            }
            break;
    }

    return result;

}
}

public string Error
{
get
{
    StringBuilder error = new StringBuilder();

// iterate over all of the properties
// of this object - aggregating any validation errors
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this);
foreach (PropertyDescriptor prop in props)
{
    String propertyError = this[prop.Name];
    if (propertyError != string.Empty)
    {
        error.Append((error.Length != 0 ? ", " : "") + propertyError);
    }
}

return error.Length == 0 ? null : error.ToString();
}
}

上面的代码是在工具提示中显示错误,当出现验证错误时,单元格变为红色。问题是使用无效值更新集合。有没有办法防止更新到集合,直到用户解决所有验证?

1 个答案:

答案 0 :(得分:0)

您可以通过cloneediting模式下输入的对象解决您的问题,当验证返回cloning object并在DataGrid Items上刷新