在 ComboBox 内的 DataGrid 中显示所选项目
我有一个 WPF 工具包 DataGrid 作为 ComboBox 模板中的下拉列表。
<toolkit:DataGrid x:Name="InnerGrid"
ItemsSource="{TemplateBinding ItemsSource}"
CanUserReorderColumns="False" CanUserResizeColumns="True"
CanUserSortColumns="False" CanUserResizeRows="False"
AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="Account" Binding="{Binding Name}" IsReadOnly="True" />
<toolkit:DataGridTextColumn Header="Description" Binding="{Binding Description}" IsReadOnly="True" />
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
当我使用下拉列表中的 DataGrid 选择一个值时,所选项目在 DataGrid 中显示为突出显示。 当我以编程方式或在关闭下拉列表时使用箭头键设置 ComboBox 的选定值时,所选项目不会在 DataGrid 中突出显示。
我需要在 DataGrid 中进行某种绑定来突出显示所选项目吗?
正如 Chris Nicol 提到的,我只需要向 ComboBox 的 SelectedItem 添加绑定,在本例中为 TemplateBinding。
<toolkit:DataGrid x:Name="InnerGrid"
ItemsSource="{TemplateBinding ItemsSource}"
SelectedItem="{TemplateBinding SelectedItem}"
CanUserReorderColumns="False" CanUserResizeColumns="True"
CanUserSortColumns="False" CanUserResizeRows="False"
AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False">
<snip>
I have a WPF toolkit DataGrid as the dropdown in a ComboBox template.
<toolkit:DataGrid x:Name="InnerGrid"
ItemsSource="{TemplateBinding ItemsSource}"
CanUserReorderColumns="False" CanUserResizeColumns="True"
CanUserSortColumns="False" CanUserResizeRows="False"
AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="Account" Binding="{Binding Name}" IsReadOnly="True" />
<toolkit:DataGridTextColumn Header="Description" Binding="{Binding Description}" IsReadOnly="True" />
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
When I select a value using the DataGrid in the dropdown, the selected item shows as highlighted in the DataGrid. When I set the selected value of the ComboBox programmatically or by using the arrow keys when the dropdown is closed, the selected item won't highlight in the DataGrid.
Is there some kind of binding I need to do within the DataGrid to highlight the selected item?
As Chris Nicol mentioned, I did just need to add a binding to the SelectedItem of the ComboBox, in this case the TemplateBinding.
<toolkit:DataGrid x:Name="InnerGrid"
ItemsSource="{TemplateBinding ItemsSource}"
SelectedItem="{TemplateBinding SelectedItem}"
CanUserReorderColumns="False" CanUserResizeColumns="True"
CanUserSortColumns="False" CanUserResizeRows="False"
AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False">
<snip>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您需要将 ComboBox 的 selectedItem 绑定到 DataGrid 的 SelectedItem 。 您可以更新问题以包含组合框吗?
Sounds like you need to bind the selectedItem of the ComboBox to the SelectedItem of the DataGrid. Can you update the question to include the comboBox?