开发者

I am using datagrid, whose itemsource is datatable. Can datatable primary key exceptions can be handled and displayed inside datagrid?

开发者 https://www.devze.com 2023-01-24 23:10 出处:网络
My grid\'s item source is a datatable that has only one field ID (Primary key). I have bound this field with Datagrid template column but at runt开发者_JAVA百科ime when I pass duplicate or null value

My grid's item source is a datatable that has only one field ID (Primary key). I have bound this field with Datagrid template column but at runt开发者_JAVA百科ime when I pass duplicate or null value to this ID inside datagrid column, no exception is caught and datagridtextbox doesn't show any error. My code is given below;

 <UserControl.Resources>
     <Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Padding" Value="-2"/>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
 </UserControl.Resources>

 <DataGrid AutoGenerateColumns="False"  Name="UserDataGrid" VerticalAlignment="Top"
            Width="381" ItemsSource="{Binding DataTableProperty, Mode=TwoWay,             NotifyOnTargetUpdated=True, NotifyOnValidationError=True}"  
             IsSynchronizedWithCurrentItem="true" >

        <DataGrid.Columns>
            <DataGridTextColumn Header="UserName" EditingElementStyle=
                                                      "{StaticResource errorStyle}">
                <DataGridTextColumn.Binding>
                    <Binding Path="ID" ValidatesOnExceptions="True"  ValidatesOnDataErrors="True" NotifyOnValidationError="True" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
                        <Binding.ValidationRules>
                            <ExceptionValidationRule ValidationStep="UpdatedValue"></ExceptionValidationRule>
                        </Binding.ValidationRules>
                    </Binding>
                </DataGridTextColumn.Binding>
            </DataGridTextColumn>
        </DataGrid.Columns>

    </DataGrid>

Can anybody please help me, what I am doing wrong ?


You probably need to validate that manually. Check WPF DataGrid Practical Examples: Validation with Bound DataSets.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号