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 6825691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:03:45+00:00 2026-05-26T22:03:45+00:00

in my Silverlight 4 application, I have a class myClass that contains a list

  • 0

in my Silverlight 4 application, I have a class myClass that contains a list of elements. This elements have a name-attribute that identifies them.

class element
{
  string Name { get; set; }
}

class myClass
{
  List<element> elements { get; }
}

To display the list of elements, I databind myClass.elements to a Silverlight-Listbox:

<ListBox>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Grid>
        <TextBox Text="{Binding Name, FallbackValue=[None], Mode=TwoWay,
                       NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
...

This way, changes to the Name of the element is automatically propagated back. But: I need to make sure, that the Name is unique in myClass.elements (and some more constraints, depending on properties of myClass.

Naturally, the elements doesn’t know who contains it (and it shouldn’t know, I think). So my problem is: How can I check the contraints of the elements (in the setter of the Name-property)? I’d really like to use the Silverlight-Databinding, because it already implements Error Notification.

Thanks in advance,
Frank

  • 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-26T22:03:46+00:00Added an answer on May 26, 2026 at 10:03 pm

    I’d suggest that element should implement INotifyProperyChange and myClass should be listening to changes, then checking for duplications, either throwing exceptions or indicating an error though IDataErrorInfo, INotifyDataErrorInfo or custom indicator. (so you implement Observer pattern, I assume, myClass here is an observer for its items).

    For custom way, it’s possible to set “IsDuplicate” property in duplicate items and bind it to background (red) color. That could be more useful for user, if s/he intentionally wants to set duplicate name here and then wants to fix some item entered before. Also if would be easier to find duplicates if you indicate all of them in the list.

    UPDATE:

    Here is indication of an error. Just changed item has border, duplicates for it – red font.

    <Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsDuplicate}" Value="True">
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, 
                   Path=(Validation.Errors)[0].ErrorContent}"/>
                <Setter Property="BorderBrush" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    Here is item template, note ValidatesOnDataErrors:

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBox Text="{Binding Path=Name, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    

    Here is your Element class:

    class Element : INotifyPropertyChanged, IDataErrorInfo
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value;
            if(PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
            }
        }
    
        private bool _isDuplicate;
        public bool IsDuplicate
        {
            get { return _isDuplicate; }
            set
            {
                _isDuplicate = value;
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsDuplicate"));
            }
        }
        //IDataErrorInfo
        public event PropertyChangedEventHandler PropertyChanged;
    
        public string this[string columnName]
        {
            get 
            {
                if (IsDuplicate)
                    return "Duplicate col" + columnName;
                return null;
            }
        }
    
        public string Error
        {
            get { return null; }
        }
    }
    

    And finally, myClass should listen to PropertyChanged and invoke duplication check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Silverlight application that is built from a set of Silverlight class
I have a Silverlight application that retreives a list of serializable classes. In these
I have a Silverlight Class Library that is being used by both Silverlight application
I have to create a publish-subscribe silverlight application. and the class that publishes the
The Scenario Currently I have a C# Silverlight Application That uses the domainservice class
in my C#-Silverlight-3-Application, I have created a class, that is doing some calculations that
We have subclassed the Silverlight Application class to add some additional functionality and then
Visual Studio 2010 RC -> Silverlight Application We have a library of images that
I have a Silverlight application that communications with an ASP.NET backend through WCF. I
I have a Silverlight application which has on it a Canvas. On that Canvas,

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.