我有一个自定义样式的组合框,效果很好。它被放置在用户控件内并绑定(bind)到数据结构。我使用 DisplayMemberPath 在 Combobox TextBox 中只显示一个元素。 ComboBox Style 取自 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="Name"!
你有解决这个问题的想法吗?
最佳答案
您不能同时设置 DisplayMemberPath
和 ItemTemplate
同时。DisplayMemberPath
用于告诉 ItemsControl 在显示对象时显示哪个属性。如果您已经传递了自定义 ItemTemplate
,则设置此字段没有意义。 ,因为您可以选择如何在该 ItemTemplate 中显示对象。
由于 MSDN 中的默认 Combobox 样式也设置了 ItemTemplate,这可能是错误的原因。
关于wpf 调试错误输出 System.WIndows.Data 错误 25,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15654935/