4

I have a ComboBox bound to a property with a specific value and "due to some specific state of data" the ComboBox doesn't contain any items (ItemsSource is empty). The ComboBox tries to find an item with the specific value using a Binding on SelectedValue property of the ComboBox, which it doesn't find and displays an error "Value '' could not be converted".

Simplified example:

ID = 80;

<ComboBox DisplayMemberPath="Value"
          SelectedValuePath="ID"
          SelectedValue="{Binding ID}">
    <ComboBox.Items>
    </ComboBox.Items>
</ComboBox>

I am aware of the state of the ComboBox and I would like to customize this message somehow eg. localized value of "The selected value could not be found, verify validity of the entries".

How can I accomplish that?

Honza Pačuta
  • 317
  • 6
  • 18

1 Answers1

2

One way is to implement your own Validation Rule like this guy did . Or inherit from IDataErrorInfo as seen in this question and this one.

Community
  • 1
  • 1
Terrance
  • 11,764
  • 4
  • 54
  • 80
  • 2
    Thanks, that put me in the right direction. I discovered that the source of the message is a default ExceptionValidationRule, that WPF uses to catch exceptions that are thrown during the update of the binding source property. I've implemented new custom ValidationRule, that checks the existence of the record in the bound collection. – Honza Pačuta May 09 '12 at 12:38