Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a WPF project with MVVM pattern. I have a Customer Entity class. I have a NewCustomer userControl whose datacontext is CustomerViewModel which has a property Customer of type Customer Entity. The usercontrol has fields like Name, Id, Country..
My requirement is the combination of Name, ID and Country should be unique, that is not other customer with the same combination should not be there in the database. So, after the user enter all the 3 fields, I have raise a command which will inturn call the service to check if the current customer is duplicate or not. If it is duplicate the 3 textboxes should have red border.

I tried by creating a LostFocus attached dependecny property, but since the TextBoxes datacontext will be Customer Entity , even after biding a command which is there in the ViewModel it is not getting fired.
Posted
Updated 8-Dec-22 9:48am
Comments
Vincent Beek 22-Nov-14 2:00am    
some code would help here.
Ramana Bellary 24-Nov-14 5:27am    
Below is my code:

public class TextBoxBehavior
{
public static DependencyProperty OnLostFocusProperty = DependencyProperty.RegisterAttached(
"OnLostFocus",
typeof(ICommand),
typeof(TextBoxBehavior),
new UIPropertyMetadata(TextBoxBehavior.OnLostFocus));

public static void SetOnLostFocus(DependencyObject target, ICommand value)
{
target.SetValue(TextBoxBehavior.OnLostFocusProperty, value);
}

///
/// PropertyChanged callback for OnDoubleClickProperty. Hooks up an event handler with the
/// actual target.
///

private static void OnLostFocus(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
var element = target as TextBox;
if (element == null)
{
throw new InvalidOperationException("This behavior can be attached to a TextBox item only.");
}

if ((e.NewValue != null) && (e.OldValue == null))
{
element.LostFocus += OnPreviewLostFocus;
}
else if ((e.NewValue == null) && (e.OldValue != null))
{
element.LostFocus -= OnPreviewLostFocus;
}
}

private static void OnPreviewLostFocus(object sender, RoutedEventArgs e)
{
UIElement element = (UIElement)sender;
ICommand command = (ICommand)element.GetValue(TextBoxBehavior.OnLostFocusProperty);
if (command != null)
{
command.Execute(e);
}
}
}
---------------
public class Customer : INotifyPropertyChanged
{

public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
//RaisePropertyChanged("Name");
}
}

private string country;
public string Country
{
get
{
return country;
}
set
{
country = value;
RaisePropertyChanged("Country");
}
}

private int id;
public int Id
{
get
{
return id;
}
set
{
id = value;
RaisePropertyChanged("Id");
}
}
}

public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

private ICommand nameChangeCommand;
public ICommand NameChangeCommand
{
get
{
if (nameChangeCommand == null)
nameChangeCommand = new RelayCommand(ValidateAndChangeName);

return nameChangeCommand;
}

}

private void ValidateAndChangeName(object obj)
{

}

private Customer customer;
public Customer Customer
{
get
{
return customer;
}
set
{
customer = value;
RaisePropertyChanged("Customer");
}
}
}
----------------

1 solution

Use Interactivity Behaviors
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900