Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7602679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:28:23+00:00 2026-05-30T23:28:23+00:00

Using the code below I can trap an invalid cell entry. In this simple

  • 0

Using the code below I can trap an invalid cell entry. In this simple example of a grocery shopping list, the GroceryItem.Name just needs to be filled in.

What I would like to do now is add the ability to validate that the entry doesn’t already exist. If it does already exist then I’d like the same cell entry highlighted but with the appropriate message. So if the user enters “Eggs” again, the cell error message should be “Eggs is already on the list”.

The item view model shouldn’t know about it’s container view model, so where can I check for a duplicate entry while still at cell validation for the “Name” property?

enter image description here

Item in the Collection

public class GroceryItem : ObservableObject, IDataErrorInfo
{

    #region Properties

    /// <summary>The name of the grocery item.</summary>
    public string Name
    {
        get { return _name; }

        set
        {
            _name = value;
            RaisePropertyChangedEvent("Name");
        }
    }
    private string _name;

    #endregion

    #region Implementation of IDataErrorInfo

    public string this[string columnName] {
        get {
            if (columnName == "Name") {
                if (string.IsNullOrEmpty(Name)) 
                    return "The name of the item to buy must be entered";
            }

            return string.Empty;
        }
    }

    public string Error { get { ... } } 

    #endregion
}

View Model holding the Collection

public class MainWindowViewModel : ViewModelBase
{

    /// <summary>A grocery list.</summary>
    public ObservableCollection<GroceryItem> GroceryList
    {
        get { return _groceryList; }

        set
        {
            _groceryList = value;
            RaisePropertyChangedEvent("GroceryList");
        }
    }
    private ObservableCollection<GroceryItem> _groceryList;

    /// <summary>The currently-selected grocery item.</summary>
    public GroceryItem SelectedItem { get; [UsedImplicitly] set; }

    void OnGroceryListChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // doing non-validation stuff 
    }
}

View Binding for DataGrid

<DataGrid 
    x:Name="MainGrid" 
    ItemsSource="{Binding GroceryList}" 
    SelectedItem="{Binding SelectedItem}" 
    ...            
    >
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="TextBlock.ToolTip" 
            Value="{Binding Error}" />
        </Style>
    </DataGrid.Resources>

    <DataGrid.Columns>
        ...
        <DataGridTextColumn Header="Item" Width="*" Binding="{Binding Name, ValidatesOnDataErrors=True}" IsReadOnly="false" />
    </DataGrid.Columns>
</DataGrid>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:28:25+00:00Added an answer on May 30, 2026 at 11:28 pm

    I don’t know if this violates MVVM but the way I would do it is pass GroceryList to GroceryItem in another constructor and save it in a private ObservableCollection groceryList in GroceryItem. It is just a reverence back to GroceryList so it does not add a much overhead.

      public class GroceryItem : ObservableObject, IDataErrorInfo
      {
            private ObservableCollection<GroceryItem> groceryList;
    
            public void GroceryItem(string Name, ObservableCollection<GroceryItem> GroceryList)
            {
                  name = Name;
                  groceryList = GroceryList;
                  // now you can use groceryList in validation
             }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In past, I am using Listview and using below code can show a particular
I'm using the code provided below to display time and date. can anyone help
i can establish a connection using HttpUrlConnection. my code below. client = new DefaultHttpClient();
i am using this code below to dynamically capture the name of the button
VS 2008 I am using the code below to detect if the client can
Using the code below to send. The gen_tcp:send call returns {error,einval} but I can't
Resource files in Silverlight can be accessed using the code below: ResourceManager rm =
I have the jquery code below that returns a list of ID's like this
As of right now I'm using this code below to download files to get
Form contains two dropdown lists created using code below. Both dropdown list elements have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.