有时当我使用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();
}
}
}
Connection
只是一个枚举,定义为:
public enum Connection
{
USB = 4,
Serial = 1,
Modem = 3,
SomethingElse = 2,
IP = 5,
AnotherThing = 6,
}
答案 0 :(得分:11)
ComboBox
周围的红色边框看起来像是Binding
中未匹配类型导致的验证错误。如果数据将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
的红色边框。