ComboBox周围出现神秘的红色边框

问题描述 投票:18回答:5

我有一个WPF应用程序 - 一个XBAP - 在主页面上有一个ComboBox。当我在ComboBox中选择一个项目时,事件处理程序会重建一个集合,该集合是列表框的数据源。看起来很简单,我之前在WPF中所做的所有事情。

这是从列表中选择项目后我的下拉列表的样子:

红色边界来自哪里?我正在从头开始构建表单,现在没有任何样式或任何内容。项目中的任何地方都没有提到“红色”字样。一旦它出现它就不会消失,并且它出现在我置于控件之上的任何东西上。

这是标记:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Converter={StaticResource ResourceKey=DeviceInfoNameConverter}}"></TextBlock>
    </DataTemplate>
</ComboBox.ItemTemplate>

更多细节:

  • 这是在IE8中运行的XBAP应用程序
  • 同一页面上的其他下拉控件不会执行此操作
  • 当我尝试使用Snoop检查控制树时,边框消失
  • 转换器不是问题的根源,我已经尝试直接绑定到底层对象上的属性,但仍然出现该框。

我对搜索到目前为止唯一的猜测是,某种默认错误模板正在应用于控件。我正在使用WIA,VS输出窗口中出现了几个COM异常,显然与ListView的数据绑定有关。控件的数据源是WIA.DeviceInfo对象,转换器只是获取下拉文本的name属性。

wpf xbap
5个回答
28
投票

Make sure whatever you're binding to is of the expected datatype.

当我绑定到decimal对象列表但我的MVVM属性类型是int时,我有这个'神秘的红盒子'。如果您正在使用它们,请检查并仔细检查所有SelectedValueDisplayMemberPathSelectedValuePath属性 - 并确保在您打算使用SelectedValue时不使用SelectedItem

在调试控制台中查找类似这样的绑定错误:

System.NotSupportedException: Int32Converter cannot convert from System.Decimal

System.Windows.Data Error: 7 : ConvertBack cannot convert value '7' (type 'Decimal'). BindingExpression:Path=SharedProductHistoryFilterCriteria.FilterDays; DataItem='PricingManagerViewModel' (HashCode=19425465); target element is 'ComboBox' (Name=''); target property is 'SelectedValue' (type 'Object') NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from System.Decimal.
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.ObjectTargetConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'

1
投票

我也有同样的问题,然后我将SelectedItem和ItemsSource的数据类型更改为double。早期我有ItemsSource的int列表和SelectedItem的双重.It工作。


0
投票

看起来它只是一个类型转换问题。对于我的情况,SelectedIndex绑定到Enum类型属性,并且从未调用该属性的setter。我只需要在转换器的Convert和ConvertBack中将返回值显式地转换为正确的类型。


0
投票

我遇到过同样的问题。结果是数据不匹配。 Combobox选择的项目被映射到一个字符串,选择的值被映射到一个字节。一旦我将两者都转换为字符串,它就按预期工作了。


-1
投票

您的selectedvalue不应该是集合的成员而不是文件夹集合吗?即Folders.Folder.ID或其他类似的东西?

所以你的CB itemsSource将是Folders,selectedItem将是Folder,selectedValue将是Name ??????

您需要将SelectedValue更新为集合的成员。另外,您需要指定ItemsSource = {Binding Path = Folders}“t。然后您需要指定DisplayMemberPath和SelectedValuePath。

我会分别测试每个绑定。首先通过删除所有项目集合的绑定来测试IsEnabled的绑定(如果已选择启用/禁用),然后开始测试绑定以进行收集。

© www.soinside.com 2019 - 2024. All rights reserved.