5

Sometimes when I view a certain window with a ComboBox, the box appears as follows:

enter image description here

Why does it appear with this red outline sometimes? Is there any way for me to get information on what is going on in the background when this occurs?

The ComboBox is defined as:

<ComboBox Grid.Column="1" Grid.Row="1" Name="cbConnectMethod" 
            ItemsSource="{Binding ConnectMethodList}" 
            SelectedItem="{Binding SelectedConnectionMethod}" 
            DisplayMemberPath="Description" VerticalAlignment="Center" 
            Width="Auto" HorizontalAlignment="Left" />

And the properties for ItemSource and SelectedItem are defined as:

public class ConnectMethod
{
    public Connection Method { get; set; }
    public string Description { get; set; }
}

public List<ConnectMethod> ConnectMethodList { get; set; }
public ConnectMethod SelectedConnectionMethod
{
    get
    {
        return ConnectMethodList.FirstOrDefault(xx => xx.Method == _dataContainer.ConnectionData.Connection);
    }
    set
    {
        if (_dataContainer.ConnectionData.Connection != value.Method)
        {
            _dataContainer.ConnectionData.Connection = value.Method;
            updateConnectDetailPage();
        }
    }
}

Connection is just an enum, defined as:

public enum Connection
{
    USB = 4,
    Serial = 1,
    Modem = 3,
    SomethingElse = 2,
    IP = 5,
    AnotherThing = 6,
}
DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • Is there any default combo style applied in the application? – Nikhil Agrawal Apr 08 '14 at 11:44
  • @NikhilAgrawal Yes, there's a style applied but this still happens when I remove the style. – DaveDev Apr 08 '14 at 11:49
  • This could be an indication of an error (an exception thrown by binding source rather than target). You can most definitely investigate it by either using snoop or just dumping your XAML. – user3455395 Apr 08 '14 at 11:57
  • @ maybe you are using focusvisualstyle for some control with red color or IDataErrorInfo and error templates. – Heena Apr 08 '14 at 12:04
  • Thanks for the suggestion to use Snoop. It's an awesome tool. I wish I knew this existed this time last year! – DaveDev Apr 08 '14 at 12:25

1 Answers1

14

The red border around your ComboBox looks like the validation error that is caused by a miss-matched type in a Binding. You can find out what the error is if you data bind the value of the first item from the Validation.Errors collection to a ToolTip. Try something like this:

<ComboBox Grid.Column="1" Grid.Row="1" Name="cbConnectMethod" 
    ItemsSource="{Binding ConnectMethodList}" 
    SelectedItem="{Binding SelectedConnectionMethod}" 
    DisplayMemberPath="Description" VerticalAlignment="Center" 
    Width="Auto" HorizontalAlignment="Left" 
    ToolTip="{Binding (Validation.Errors)[0].ErrorContent, 
    RelativeSource={RelativeSource Self}}" />

Once you can see what the error is, then you will be able to address it and fix your problem, which will in turn remove the red border from your ComboBox.

Sheridan
  • 68,826
  • 24
  • 143
  • 183