为什么我的DataGridCell拒绝所有输入?

时间:2019-01-29 09:12:33

标签: c# wpf validation xaml datagridcell

我有两个TabItem,每个TabItem包含一个DataGrid,并且在它们两个中都实现了INotifiyDataErrorInfo。这两个DataGrid都由具有基础模型类(POCO)的ViewModel填充。 在第一个TabItem中,一切都按要求工作,但在第二个TabItem中,DataGrid仅在经过验证的字段/单元格中接受输入。其他/未验证的字段/单元格可以让我输入数据,但是当单元格失去焦点时,新输入的字符将被丢弃/拒绝。

可能是什么原因?

有效的TabItem / DataGrid:

class ShipmentRuleCreationValidation
{
    public function validate($request)
    {
        $days = ['mon','tue','wed','thu','fri','sat','sun'];
        for ($i=0; $i<count($request['rule']); $i++ ){
            if($request['rule'][$i]['attribute'] == "day"){
                if(!in_array($request['rule'][$i]['value'],$days)){
                    throw new \Illuminate\Validation\ValidationException(['rule.'.$i.'value' => 'Invalid day value']);
                }
            }
        }

    }
}

有效的TabItem / DataGrid:

<UserControl x:Class="ConfigTool.Controls.RawTagTabItem"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:ct_ctrls="clr-namespace:ConfigTool.Controls"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">

<Grid>
    <ct_ctrls:CTDataGrid x:Name="tagsGrid" ItemsSource="{Binding}" GridLinesVisibility="Vertical" AlternatingRowBackground="#C3DDE5" 
                         AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" 
                         SelectionUnit="FullRow" SelectionMode="Extended" BorderThickness="3" CellEditEnding="CellEditingEnds" RowStyle="{StaticResource RawTagDataGridRow}">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="TagName" Header="Tag" Width="*" 
                                Binding="{Binding Mode=TwoWay, Path=RawTag.TagName, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"
                                ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}" />
            <DataGridTextColumn x:Name="TagCycle" Header="Cycle" 
                                Binding="{Binding Mode=TwoWay, Path=RawTag.Cycle, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" 
                                ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}">
            </DataGridTextColumn>
            <DataGridTextColumn x:Name="TagSource" Header="Source" Width="*" 
                                Binding="{Binding Mode=TwoWay, Path=RawTag.Source, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"
                                ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}"/>
            <DataGridTextColumn x:Name="Unassigned" Header="unassigned" Width="*" 
                                Binding="{Binding Mode=OneWay, Path=RawTag.Unassigned, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=False}"
                                ElementStyle="{StaticResource ResourceKey=textBlockUnassignedStyle}"/>
            <DataGridTemplateColumn x:Name="editTagColumn" Header="" CanUserResize="True" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <WrapPanel>
                            <Button x:Name="btnTagDelete" Click="BtnTagDelete_Click" CommandParameter="{Binding}" Height="15" Width="15" Margin="2">
                                <Button.Content>
                                    <Image Source="../Resources/delete.png"></Image>
                                </Button.Content>
                            </Button>
                        </WrapPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </ct_ctrls:CTDataGrid>
</Grid>

在很好地读取CSV文件后,DataGrid已填充...仅试图修改(未经验证的)单元(实际上是DataGridTextColumns和DataGridCheckboxColumns)会导致问题。

1 个答案:

答案 0 :(得分:0)

这很简单:正确执行即可! ;-) 我只是忘记在拒绝输入的单元格的绑定中添加 UpdateSourceTrigger = PropertyChanged