WPF Datagrid DeferRefresh异常

时间:2016-05-30 15:37:30

标签: c# wpf datagrid

这是我的DataGrid

<DataGrid x:Name="FixedPositionDataGrid" ItemsSource="{Binding FixedPositionList}" HorizontalAlignment="Left" Margin="33,132,0,0" VerticalAlignment="Top" Width="227" Height="128" AutoGenerateColumns="False" FontSize="12" CanUserAddRows="False" CellEditEnding="FixedPositionDataGrid_CellEditEnding" CanUserSortColumns="False" PreviewKeyDown="FixedPositionDataGrid_PreviewKeyDown">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="IsEnabled" Value="{Binding IsEditable}"></Setter>
        </Style>
    </DataGrid.RowStyle>

    <DataGrid.Columns>
        <DataGridTextColumn Header="Indice" Binding="{Binding Index}" IsReadOnly="True" Width="80"/>
        <DataGridTextColumn Header="%" Width="130">
            <DataGridTextColumn.Binding>
                <Binding Path="Percentage" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <mynamespace:PercentageValidationRule Min="0" Max="100"/>
                    </Binding.ValidationRules>
                </Binding>
            </DataGridTextColumn.Binding>
        </DataGridTextColumn>
    </DataGrid.Columns>

</DataGrid>

这是ValidationRule:

public class PercentageValidationRule : ValidationRule
{
    private int _min;
    private int _max;

    public PercentageValidationRule()
    {
    }

    public int Min
    {
        get { return _min; }
        set { _min = value; }
    }

    public int Max
    {
        get { return _max; }
        set { _max = value; }
    }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        int percentage = 0;

        try
        {
            if (((string)value).Length > 0)
                percentage = Int32.Parse((String)value);
        }
        catch (Exception e)
        {
            return new ValidationResult(false, "Illegal characters or " + e.Message);
        }

        if ((percentage < Min) || (percentage > Max))
        {
            return new ValidationResult(false,
              "Please enter a value in the range: " + Min + " - " + Max + ".");
        }
        else
        {
            return new ValidationResult(true, null);
        }
    }
}

插入错误值(即值&lt; 0或&gt; 100或字符串)后,我会看到&#34;!&#34;细胞有红色边框;如果我关闭窗口(使用任何按钮)并尝试重新打开它,我有一个例外

'DeferRefresh' is not allowed during an AddNew or EditItem transaction

在另一个DataGrid中,我有一个类似的XAML布局,带有DataGridTextColumns 但没有限制,错误也没有出现。

我在SO中看到了其他一些问题,但没有解决方案适合我

0 个答案:

没有答案