<p>I'm trying to write a custom WPF ValidationRule to enforce that a certain property is unique within the context of a given collection. For example: I am editing a collection of custom objects bound to a ListView and I need to ensure that the Name property of each object in the collection is unique. Does anyone know how to do this?</p>

[{'answer_id': 34885, 'author': 'Daniel Jennings', 'author_id': 3641, 'author_profile': 'https://Stackoverflow.com/users/3641', 'pm_score': 3, 'selected': True, 'text': '<p>First, I\'d create a simple DependencyObject class to hold your collection: </p>\n\n<pre><code>class YourCollectionType : DependencyObject {\n\n    [PROPERTY DEPENDENCY OF ObservableCollection&lt;YourType&gt; NAMED: BoundList]\n\n}\n</code></pre>\n\n<p>Then, on your ValidationRule-derived class, create a property:</p>\n\n<pre><code>YourCollectionType ListToCheck { get; set; }\n</code></pre>\n\n<p>Then, in the XAML, do this:</p>\n\n<pre><code>&lt;Binding.ValidationRules&gt;\n    &lt;YourValidationRule&gt;\n       &lt;YourValidationRule.ListToCheck&gt;     \n          &lt;YourCollectionType BoundList="{Binding Path=TheCollectionYouWantToCheck}" /&gt;\n       &lt;/YourValidationRule.ListToCheck&gt;\n    &lt;/YourValidationRule&gt;\n&lt;/Binding.ValidationRules&gt;\n</code></pre>\n\n<p>Then in your validation, look at ListToCheck\'s BoundList property\'s collection for the item that you\'re validating against. If it\'s in there, obviously return a false validation result. If it\'s not, return true.</p>\n'}, {'answer_id': 46980091, 'author': 'Patrick', 'author_id': 278889, 'author_profile': 'https://Stackoverflow.com/users/278889', 'pm_score': 1, 'selected': False, 'text': '<p>I would only create a custom dependency object if there were other properties I wanted to bind to the rule. Since in this case all we\'re doing is attaching a single collection of values to check against, I made my <code>&lt;UniqueValueValidationRule.OtherValues&gt;</code> property a <code>&lt;CollectionContainer&gt;</code>.</p>\n\n<p>From there, to get past the problem of the <code>DataContext</code> not being inherited, <code>&lt;TextBox.Resources&gt;</code> needed to have a <code>&lt;CollectionViewSource&gt;</code> to hold the actual binding and give it a <code>{StaticResource}</code> key, which <code>OtherValues</code> could then use as binding source.</p>\n\n<p>The validation rule itself then need only loop through <code>OtherValues.Collection</code> and perform equality checks.</p>\n\n<p>Observe:</p>\n\n<pre><code>    &lt;TextBox&gt;\n        &lt;TextBox.Resources&gt;\n            &lt;CollectionViewSource x:Key="otherNames" Source="{Binding OtherNames}"/&gt;\n        &lt;/TextBox.Resources&gt;\n        &lt;TextBox.Text&gt;\n            &lt;Binding Path="Name"&gt;\n                &lt;Binding.ValidationRules&gt;\n                    &lt;t:UniqueValueValidationRule&gt;\n                        &lt;t:UniqueValueValidationRule.OtherValues&gt;\n                            &lt;CollectionContainer Collection="{Binding Source={StaticResource otherNames}}"/&gt;\n                        &lt;/t:UniqueValueValidationRule.OtherValues&gt;\n                    &lt;/t:UniqueValueValidationRule&gt;\n                &lt;/Binding.ValidationRules&gt;\n            &lt;/Binding&gt;\n        &lt;/TextBox.Text&gt;\n    &lt;/TextBox&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/34802', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/317/']