Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my ComboBox have a red outline?

Tags:

c#

combobox

wpf

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,
}
like image 669
DaveDev Avatar asked Dec 11 '22 06:12

DaveDev


1 Answers

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.

like image 54
Sheridan Avatar answered Dec 13 '22 19:12

Sheridan