WPF ComboBox数据绑定异常求助:控件显示System.Windows.Style
问题:WPF ComboBox绑定数据源异常,显示
System.Windows.Style 问题现象
- ComboBox控件未显示预期的枚举描述文本,反而显示
System.Windows.Style LineTypes静态属性的getter断点未触发,说明数据源绑定未生效
当前代码
WPF窗口XAML代码
<mah:MetroWindow x:Class="AlarmConfiguration.Lines.LineSettingsRadio" xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:AlarmConfiguration.Lines" xmlns:resx="clr-namespace:AlarmConfiguration.Resources" d:DataContext="{d:DesignInstance Type=local:Line}" mc:Ignorable="d" Title="{x:Static resx:Translations.RemoteConfig}" Height="220" Width="320" Closed="MetroWindow_Closed"> <StackPanel Orientation="Vertical"> <CheckBox Content="Linia 24h"/> <ComboBox SelectedItem="{Binding Type, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"> <Style TargetType="{x:Type ComboBox}"> <Setter Property="ItemsSource" Value="{Binding LineTypes}" /> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </ComboBox> </StackPanel> </mah:MetroWindow>
Line类C#代码
[MarshalAs(UnmanagedType.I1)] public LineType lineType = LineType.NotUsed; public string Type { get => Converter.EnumDescription.GetDescription(thisLine.lineType); set { foreach (var lineType in Enum.GetValues(typeof(LineType)).Cast<LineType>()) { if (Converter.EnumDescription.GetDescription(lineType) == value) { thisLine.lineType = lineType; break; } } OnPropertyChanged(); } } public static IEnumerable<string> LineTypes { get { yield return Converter.EnumDescription.GetDescription(LineType.NotUsed); foreach (var lineType in Enum.GetValues(typeof(LineType)).Cast<LineType>()) { if(lineType != LineType.NotUsed) yield return Converter.EnumDescription.GetDescription(lineType); } } } public enum LineType : byte { [LocalizedDescription("LineTypeEntryExit1", typeof(Translations))] Enter_Exit_1 = 0, [LocalizedDescription("LineTypeEntryExit2", typeof(Translations))] Enter_Exit_2 = 1, [LocalizedDescription("LineTypeInternal", typeof(Translations))] Internal = 2, [LocalizedDescription("LineTypeInstant", typeof(Translations))] Instant = 3, //[LocalizedDescription("LineTypeJointly", typeof(Translations))] Jointly = 4, [LocalizedDescription("LineTypeNotUsed", typeof(Translations))] NotUsed = 5, [LocalizedDescription("LineTypeFire24", typeof(Translations))] Fire24 = 6, [LocalizedDescription("LineTypeBreakingIn24", typeof(Translations))] BreakingIn24 = 7, }
正常工作的DataGridComboBoxColumn示例
<DataGridComboBoxColumn Header="{x:Static resx:Translations.ZoneAudiableType}" SelectedItemBinding="{Binding Sound, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" Width="150"> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="{x:Type ComboBox}"> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="ItemsSource" Value="{Binding SoundTypes}" /> </Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="{x:Type ComboBox}"> <Setter Property="ItemsSource" Value="{Binding SoundTypes}" /> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn>
解决方案
核心问题分析
- Style放置错误:直接将
Style作为ComboBox的子元素,会被ComboBox识别为Items集合的一部分,导致显示System.Windows.Style文本 - 静态属性绑定错误:
LineTypes是静态属性,直接用{Binding LineTypes}无法找到该属性,必须通过x:Static指定绑定源
修正后的ComboBox代码
方式一:使用Style属性包裹Style定义
<ComboBox SelectedItem="{Binding Type, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"> <ComboBox.Style> <Style TargetType="{x:Type ComboBox}"> <Setter Property="ItemsSource" Value="{Binding Source={x:Static local:Line.LineTypes}}" /> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </ComboBox.Style> </ComboBox>
方式二:直接在ComboBox上设置属性(更简洁)
<ComboBox SelectedItem="{Binding Type, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" ItemsSource="{Binding Source={x:Static local:Line.LineTypes}}" VerticalAlignment="Center"/>
修改后,ComboBox会正常加载LineTypes中的枚举描述文本,LineTypes的getter断点也会触发,绑定逻辑正常生效。
内容的提问来源于stack exchange,提问作者Daniel
相关产品推荐
相关产品推荐

