开发者

Mysterious red border appears around ComboBox

I have a WPF app - an XBAP - with a ComboBox on the main page. When I select an item in the ComboBox, an event handler rebuilds a collection which is the data source for a listbox. Seems like pretty simple stuff, all stuff I've done in WPF before.

This is what my dropdown looks like after selecting an item from the list:

Mysterious red border appears around ComboBox

Where on earth did the red border come from? I am b开发者_JAVA技巧uilding the form from scratch, there is no styling or anything on it right now. The text "red" is not even mentioned anywhere in the project. It will not go away once it appears, and it shows up over anything that I place on top of the control.

Here's the markup:

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

Some more details:

  • This is an XBAP application running in IE8
  • Other dropdown controls on the same page are not doing this
  • The border disappears when I attempt to examine the control tree with Snoop
  • The converter is not the source of the problem, I've tried binding directly to a property on the underlying object and the box still appears.

My only guess so far from searching is that there is some sort of default error template that is being applied to the control. I'm working with WIA, and there are several COM exceptions that appear in the VS output window, apparently related to the databinding for the ListView. The data source to the control is a WIA.DeviceInfo object, the converter is just getting the name property for the dropdown text.


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

I had this 'mysterious red box' when I tried to bind a list of decimal objects but my MVVM property type was int. Check and double check all your SelectedValue, DisplayMemberPath, and SelectedValuePath properties if you're using them - and make sure you're not using SelectedValue when you meant to use SelectedItem.

Look in the debug console for binding errors like this :

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)'


I too had the same issue, then I changed data types of both SelectedItem and ItemsSource to be double .Earlier I had list of int for ItemsSource and double for SelectedItem.It works.


Looks like it's simply a type cast issue. For my case, the SelectedIndex was bound to an Enum type property and the setter of that property was never called. I just had to explicitly cast the returned value to the correct type in both Convert and ConvertBack of my converter.


I had the same issue. It turned out to be a data mismatch. Combobox selected item was mapped to a string and selected value to a byte. Once I converted both to string it was working as expected.


Should your selectedvalue not be a member of the collection and not the Folders collection iteself? i.e Folders.Folder.ID or something along those lines?

So your CB itemsSource would be Folders and selectedItem would be Folder and selectedValue would be Name??????

you need to update the SelectedValue to a member of a collection. Additionally you would need to specify ItemsSource = {Binding Path=Folders}" t. Then you would need to specify DisplayMemberPath and SelectedValuePath.

I would test each binding separately. First test binding for IsEnabled by removing every binding for collection of items if it is enabled / disabled as selected then start testing for the binding for collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜