我有一个定制风格的ComboBox工作正常.它放在usercontrol中并绑定到数据结构.我使用displayMemberPath只显示ComboBox TextBox中的一个元素. ComboBox样式取自MSDN并多次使用.所以这里没有显示.
<UserControl x:Class="wpf.projext1.MyComboBox" x:Name="MyControl" ... <ComboBox Style="{StaticResource ComboBoxStyle}" Text="{Binding ElementName=MyControl,Path=Text}" IsEditable="True" IsTextSearchEnabled="False" StaysOpenOnEdit="True" ItemsSource="{Binding ElementName=MyControl,Path=MyItemsSource}" displayMemberPath="Name" </ComboBox
我得到以下令人讨厌的错误消息填充输出窗口:
System.Windows.Data Error: 25 : Both 'ContentTemplate' and 'ContentTemplateSelector' are set; 'ContentTemplateSelector' will be ignored. ComboBoxItem:'ComboBoxItem' (Name='')
如果我遗漏了
displayMemberPath="Name"
…没有显示关于错误25的调试输出.但我绝对需要DiplayMemberPath =“名字”!
你有想法解决这个问题吗?
您不能同时设置displayMemberPath和ItemTemplate.
displayMemberPath用于告诉ItemsControl在显示对象时要显示的属性.如果您已经传递了自定义ItemTemplate,那么设置此字段就没有了,因为您可以选择如何在该ItemTemplate中显示该对象.
由于MSDN的默认ComboBox样式也设置了ItemTemplate,这可能是导致错误的原因.