Problem on validation in Listview

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;
}

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

}

[4425 byte] By [Makube] at [2008-1-9]
# 1

try this in your listview

<ListView KeyboardNavigation.TabNavigation="Continue"............

MarlonGrech at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

Thank you for your advice.

I have tried it, but it seems not work. Should I need to customize one validtation rule class?

Makube at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

I have made clear of some problem, but still can not solve the 2 points

1. When error occurs, the tooltip always shows "Exception has been thrown by the target of invocation" instead of proper content defined in the exception string (In this case, it should be "Invalid location name")

2. Validation does not work whenever you input expected value or invalid value (ie.When you input "America" or "France" in the textfield, then lose keyboard focus, textfield is still wrapped with red rectangle which indicate it is an invalid value)

Could anyone give me suggestion? thanks

Makube at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

I have solved the first one problem

Is anyone knows the second one?

Makube at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

what type of updatesourcetrigger are you using?
MarlonGrech at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 6

Now I used "PropertyChanged" as the type of updatesourcetrigger, and I have solved all problems.

Anyway, thanks

Makube at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified