在 WPF ComboBox 中显示自定义 DisplayMember
这是我的组合框:
WeeklyStartDate 和 WeekNumber 我想在 DisplayMember 属性中显示。但 WPF 说我无法使用 DisplayMember,因为我已经使用了 ItemTemplate...
当我单击 ComboBox 中的项目时,如何使用以下代码显示自定义 DisplayMember ?
目前它正在使用 ToString() 方法来渲染 DisplayMember,显示 XXX.ViewModel.WeeklyDateViewModel,这是名称空间+类型。
<ComboBox
Style="{StaticResource ComboBoxStyle1}"
AlternationCount="2"
FontSize="16"
VerticalContentAlignment="Center"
Width="150"
IsEditable="True"
SelectedItem="{Binding SelectedWeeklyDateViewModel,Mode=TwoWay}"
ItemContainerStyle="{StaticResource alternateColor}"
ItemsSource="{Binding WeeklyDatesList}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VirtualizingStackPanel.VirtualizationMode="Recycling" />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="100" Foreground="blue" Text="{Binding WeekStartDate,Mode=TwoWay, StringFormat='yyyy-MM-dd'}" />
<TextBlock Text=" " />
<TextBlock Width="100" Foreground="Red" Text="{Binding WeekNumber}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
this is my ComboBox:
WeeklyStartDate and WeekNumber I want to display in the DisplayMember property. But WPF says I can not use DisplayMember as I use already an ItemTemplate...
How can I display with the below code a custom DisplayMember when I clicked on an item in the ComboBox ?
At the moment it is using the ToString() method to render the DisplayMember showing XXX.ViewModel.WeeklyDateViewModel which is the namespace+type.
<ComboBox
Style="{StaticResource ComboBoxStyle1}"
AlternationCount="2"
FontSize="16"
VerticalContentAlignment="Center"
Width="150"
IsEditable="True"
SelectedItem="{Binding SelectedWeeklyDateViewModel,Mode=TwoWay}"
ItemContainerStyle="{StaticResource alternateColor}"
ItemsSource="{Binding WeeklyDatesList}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VirtualizingStackPanel.VirtualizationMode="Recycling" />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="100" Foreground="blue" Text="{Binding WeekStartDate,Mode=TwoWay, StringFormat='yyyy-MM-dd'}" />
<TextBlock Text=" " />
<TextBlock Width="100" Foreground="Red" Text="{Binding WeekNumber}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速而肮脏的方法:重写 WeeklyDateViewModel 上的 ToString 以返回要显示的字符串
Quick and dirty method: override ToString on your WeeklyDateViewModel to return the string you want to display