c# – 为什么我的ComboBox有一个红色轮廓?

有时当我使用ComboBox查看某个窗口时,该框显示如下:

为什么有时会出现这个红色轮廓?当发生这种情况时,我有什么方法可以获得有关背景情况的信息吗?

ComboBox定义为:

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

ItemSource和SelectedItem的属性定义为:

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();
        }
    }
}

连接只是一个枚举,定义如下:

public enum Connection
{
    USB = 4,
    Serial = 1,
    Modem = 3,
    SomethingElse = 2,
    IP = 5,
    AnotherThing = 6,
}

解决方法:

ComboBox周围的红色边框看起来像是绑定中未匹配类型导致的验证错误.如果数据将Validation.Errors集合中第一个项的值绑定到ToolTip,则可以找出错误是什么.尝试这样的事情:

<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}}" />

一旦你可以看到错误是什么,那么你将能够解决它并修复你的问题,这将从你的ComboBox中删除红色边框.

上一篇:JavaFX中的表格视图单元格中的ComboBox


下一篇:Android-文件存储-文本存储