【问题标题】:Show selected item in a DataGrid within a ComboBox在 ComboBox 内的 DataGrid 中显示所选项目
【发布时间】:2010-11-07 01:29:08
【问题描述】:

我有一个 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>

【问题讨论】:

    标签: wpf datagrid combobox wpftoolkit


    【解决方案1】:

    听起来您需要将 ComboBox 的 selectedItem 绑定到 DataGrid 的 SelectedItem。您能否更新问题以包含组合框?

    【讨论】:

    • 对不起,我本来可以更清楚的。它实际上是一个 ComboBox 模板。没错,我确实只需要绑定 SelectedItem。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2020-10-18
    • 2010-11-30
    • 2014-03-13
    • 2011-04-19
    • 2015-09-28
    相关资源
    最近更新 更多