组合框的DisplayMemberPath属性未按预期工作,源是具有自定义键的字典

0 wpf combobox

我有一个ComboBox,它的源是一个IDictionary<Key, String>'Key'是自定义键.的ComboBox定义如下:

<ComboBox 
    x:Name="MD_PDIR_COMBO_SOURCE"
    Grid.Row="0" 
    Style="{DynamicResource USButtonComboBoxStyle}"
    Margin="14,5"
    VerticalAlignment="Top" 
    HorizontalAlignment="Stretch"
    ItemsSource="{Binding Path=SourcesComboList}"
    DisplayMemberPath="Value"
    SelectedValuePath="Key"
    SelectedValue="{Binding Path=SelectedSource}"
    SelectionChanged="MD_PDIR_COMBO_SOURCE_SelectionChanged" 
/>                        
Run Code Online (Sandbox Code Playgroud)

现在我面临的问题是,虽然DisplayMemberPath设置为字典的"值"是a String,但在选择项目时组合框中显示的可视文本是BLANK/EMPTY.

尽管组合框选择更改的所有所需功能都正确发生,但在选择项目后未显示值/文本.

请帮忙!

pro*_*ica 7

这个工作还可以

public string Value{ get; set; }
Run Code Online (Sandbox Code Playgroud)

这不行!

public string Value;
Run Code Online (Sandbox Code Playgroud)

结论: DisplayMemberPath 指示项目对象的属性而不是字段。使用 getter 和 setter:{ get; 放; }


Rac*_*hel 6

你的风格是否ItemTemplate完全适用?

设定DisplayMemberPath为称的快捷方式ItemTemplate应该是TextBlock与它的Text必然无论是在DisplayMemberPath文字,因此设置ItemTemplate除了DispalyMemberPath将覆盖它,使DisplayMemberPath无用