在 ComboBox 内的 DataGrid 中显示所选项目

发布于 2024-07-26 00:15:08 字数 1478 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2024-08-02 00:15:08

听起来您需要将 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文