Quantcast
Channel: Problem on validation in Listview
Viewing all articles
Browse latest Browse all 8

Problem on validation in Listview

0
0

I have data bound listview, and listview is binding to one object class "Records" (the properties of this object is assigned instantly with data which come from an external file source). The listview could be editable and so one custom control "EditBox"is built in it. Now I want to check validation for each editable cell in the listview when user input the value. I use ValidationRules Class for checking. As long as the input value is invalid, after editable cell lost focus(i.e.turn to normal model), it should show some red rectangle and tooltip for reminder. 

 

Now the problem occurs that the red rectangle automatically appears during the process of editing the cell, also the tooltip shows "Exception has been thrown by the target of invocation" instead of proper content. But when I add "UpdateSourceTrigger="LostForcus" in the binding datafield, the validation never happens (i.e. no rectangle and tooltip any more).

 

Is there anyone know the reason and how to fix that? Thanks

 

In xaml, I write styling for the EditBox and data binding for one property "Location" of object class "Records"

======================================================================

 <Window.Resources>
 
 <Style x:Key="{x:Type l:EditBox}" TargetType="{x:Type l:EditBox}" >
    ......
   <Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <DockPanel LastChildFill="True">
               <TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="12pt" FontWeight="Bold">
               !!
               </TextBlock>
           <Border BorderBrush="Red" BorderThickness="1">
               <AdornedElementPlaceholder />
           </Border>
           </DockPanel>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
  <Style.Triggers>
     <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={RelativeSource Self},
                    Path=(Validation.Errors)[0].ErrorContent}"/>
      </Trigger>
   </Style.Triggers>
 </Style>
 </Window.Resources>

 

<ListView x:Name="MyListView" IsSynchronizedWithCurrentItem="True">
      <GridViewColumn Header="Location">
             <GridViewColumn.CellTemplate>
               <DataTemplate>
                   <l:EditBox Height="20">
                       <l:EditBox.Value>
                           <Binding Path="Location" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
                                  <Binding.ValidationRules>
                                        <ExceptionValidationRule/>
                                 </Binding.ValidationRules>
                           </Binding>
                      </l:EditBox.Value>
                 </l:EditBox>
              </DataTemplate>
           </GridViewColumn.CellTemplate>
     </GridViewColumn>

 </ListView>

===========================================================================

In code-behind, I write Exception for the "Location" property and some method for turn editable model to normal model in the EditBox class

 

public class Records: INotifyPropertyChanged

{

public string Location
{
   get { return location; }
   set 
   { 
        location = value;
        OnPropertyChanged("Location");

        if ((value != "America") || (value != "France"))
                throw new Exception("Invalid Location Name");
   }
}

}

 

public class EditBox: Control

{

        .................

//Turn editable model to normal model when press Enter Key or F2

        private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
        {
            if (IsEditing && (e.Key == Key.Enter || e.Key == Key.F2))
            {
                IsEditing = false;
                _canBeEdit = false;
            }
        }

        //Turn editable model to normal model when Keyboard lost focus
        private void OnTextBoxLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            IsEditing = false;
        }

        ..................

}


Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images