3

I have a user control and i use (IDataErrorInfo) for validation the problem is when i load User control Dynamically the validation not appears for the first time but when i load it in design time it works what is the problem ? here is the user control

 <UserControl x:Class="WeaponLibrary.WeaponUserControl"
         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:local="clr-namespace:WeaponLibrary"
         xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
         xmlns:ad="clr-namespace:DataAccessClassLibrary.ValidationRules;assembly=DataAccessClassLibrary"
         mc:Ignorable="d"
         d:DataContext="{d:DesignInstance local:WeaponUserControlViewModel}"
        Height="700" Width="730" FlowDirection="RightToLeft" Loaded="UserControl_Loaded" >
<AdornerDecorator>
    <Expander  IsExpanded="True" >
        <DockPanel>
            <WrapPanel DockPanel.Dock="Top"   DataContext="{Binding Path=SelectedWeapon}">
                <StackPanel Width="315" Margin="20,0,10,0">

                    <ComboBox x:Name="CmbWeaponType" IsReadOnly="True" IsEditable="True"
                          ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, 
                          AncestorType={x:Type UserControl}}, Path=DataContext.WeaponTypeList,ValidatesOnNotifyDataErrors =True,ValidatesOnDataErrors=True,
                    NotifyOnValidationError=True,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="weaponTypeName" SelectedValuePath="ID" >
                        <ComboBox.SelectedValue>
                            <Binding Path="weaponTypeID" Mode="TwoWay" 
                                 UpdateSourceTrigger="PropertyChanged" ValidatesOnNotifyDataErrors="True" NotifyOnValidationError="True">
                            </Binding>
                        </ComboBox.SelectedValue>
                    </ComboBox>
                </StackPanel>
                <StackPanel Width="315" Margin="20,0,10,0" >

                    <TextBox x:Name="TxtWeaponNumber" Width="315" Text="{Binding weaponNumber,UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True ,ValidatesOnNotifyDataErrors=True,ValidatesOnDataErrors=True,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True,ValidatesOnExceptions=True}"  >
                        <!--<TextBox.Text>
                            <Binding Path="weaponNumber">
                                <Binding.ValidationRules >
                                    <ad:RequiredRule ValidatesOnTargetUpdated="True"  />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox.Text>-->
                    </TextBox>
                </StackPanel>
                <StackPanel Width="315" Margin="20,0,10,0">

                    <TextBox x:Name="TxtOwnership" Width="315"
                         controls:TextBoxHelper.IsWaitingForData="True"
                         Text="{Binding Path=ownership}" />
                </StackPanel>
                <StackPanel Width="315" Margin="20,0,10,0">

                    <TextBox x:Name="TxtMagazineNumber" Width="315"
                         controls:TextBoxHelper.IsWaitingForData="True"
                         Text="{Binding Path=magazineNumber}" />
                </StackPanel>
                <StackPanel>

                    <TextBox x:Name="TxtNotes" Margin="20,0,10,0" Width="660" Height="100"
                         controls:TextBoxHelper.IsWaitingForData="True" Text="{Binding Path=note}" />
                </StackPanel>
                <StackPanel Width="660" Margin="10,10,10,0">
                    <TextBlock Text="{Binding AllErrors}"  Foreground="Red" >

                    </TextBlock>
                </StackPanel >


            </WrapPanel>
            <DataGrid x:Name="DgvWeapons" Margin="0,10,0,10" VerticalContentAlignment="Center"  ItemsSource="{Binding WeaponsList, Mode=OneWay,ValidatesOnNotifyDataErrors=False,NotifyOnValidationError=False}"  
                  HorizontalContentAlignment="Center" IsReadOnly="True"  AlternatingRowBackground="{DynamicResource AccentColorBrush4}" SelectionMode="Single" 
                  CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False" Width="657"  SelectedItem="{Binding SelectedWeapon,ValidatesOnNotifyDataErrors=False,NotifyOnValidationError=False}" MouseLeftButtonDown="DgvWeapons_MouseLeftButtonDown">
                <DataGrid.Columns>
                    <DataGridTextColumn Width="100" Binding="{Binding Path=WeaponTypeName}" />
                    <DataGridTextColumn  Width="100" Binding="{Binding Path=weaponNumber}"/>
                    <DataGridTextColumn  Width="100" Binding="{Binding Path=ownership}"/>
                    <DataGridTextColumn  Width="100" Binding="{Binding Path=magazineNumber}"/>
                    <DataGridTextColumn  Width="100" Binding="{Binding Path=note}"/>
                </DataGrid.Columns>
            </DataGrid>
        </DockPanel>
    </Expander>
</AdornerDecorator>
</UserControl>

here is the code:

var tabItem = new TabItem { Header = item.FormName };
var adornerDecorator = new AdornerDecorator();
var stackPanel = new StackPanel();
stackPanel.Children.Add(item.MainForm);
adornerDecorator.Child = stackPanel;
tabItem.Content = adornerDecorator;
tabItem.SetValue(ControlsHelper.HeaderFontSizeProperty, 18.0);
MainTabControl.Items.Add(tabItem);

here is the code of implementation

    public string Error
    {
        get {return string.Empty; }
    }

    public string this[string columnName]
    {
        get
        {
            if (columnName == "weaponNumber")
            {
                bool valid = !string.IsNullOrEmpty(weaponNumber);

                if (!valid)
                {
                    var propertyErrors = new List<string>();
                    propertyErrors.Add("error message");
                    SetErrors("weaponNumber", propertyErrors);
                    return "error message";
                }
                else
                {
                    ClearErrors("weaponNumber");
                }
                OnPropertyChanged("AllErrors");
            }

            if (columnName == "weaponTypeID")
            {
                bool valid = weaponTypeID != null && weaponTypeID != new Guid();

                if (!valid)
                {
                    var propertyErrors = new List<string>();
                    propertyErrors.Add("error message");
                    SetErrors("weaponTypeID", propertyErrors);
                }
                else
                {
                    ClearErrors("weaponTypeID");
                }
                OnPropertyChanged("AllErrors");
                return "error message";
            }

            return null;
        }
    }
am shammout
  • 195
  • 3
  • 11
  • If you have a problem with `IDataErrorInfo`, do you not think it's a good idea to show your relevant implementation code? – Sheridan Mar 26 '15 at 08:58
  • I edit the post and add the implementation – am shammout Mar 26 '15 at 09:14
  • I have the same issue. Is it resolved? – Manprit Singh Sahota Mar 06 '18 at 19:15
  • Try to come up with a minimal reproducing sample. It's difficult to help w/o being able to reproduce. Note that most validation issues come from the fact errors are stored in the object instead they should be computed on demand. Check my answer here: https://stackoverflow.com/questions/34665650/force-inotifydataerrorinfo-validation – Simon Mourier Mar 06 '18 at 22:37

0 Answers0