【问题标题】:WPF Use DataGrid With DataGridTemplateColumn thas has a ComboBox with Datagrid insideWPF 使用带有 DataGridTemplateColumn 的 DataGrid,它有一个 ComboBox,里面有 Datagrid
【发布时间】:2021-05-01 10:57:39
【问题描述】:

我有一个 T 列表绑定到数据网格的情况。在集合中有一个属性也是一个集合。这是很常见的。但是,在这种情况下,我想使用我设置样式的数据网格 UI 对象并将其放入 ComboBox 中,因为我发现在数据网格列中有一个下拉项非常好。希望这是有道理的,用代码来展示我的故事:

<DataGrid
    AutoGenerateColumns="False"
    CanUserAddRows="False"
    CanUserDeleteRows="False"
    ColumnWidth="*"
    HeadersVisibility="Column"
    ItemsSource="{Binding MyCollection}">

    <DataGrid.Columns>
        <DataGridCheckBoxColumn Binding="Property1" />
        <DataGridCheckBoxColumn Binding="Property2" />
        <DataGridCheckBoxColumn Binding="Property3" />
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox>
                        <DataGrid
                            AutoGenerateColumns="False"
                            CanUserAddRows="False"
                            CanUserDeleteRows="False"
                            ColumnWidth="*"
                            HeadersVisibility="Column"
                            ItemsSource="{Binding MyObjectPropertyCollection}">

                            <DataGrid.Columns>
                                <DataGridCheckBoxColumn Binding="SubProperty1" />
                                <DataGridCheckBoxColumn Binding="SubProperty2" />
                                <DataGridCheckBoxColumn Binding="SubProperty3" />
                            </DataGrid.Columns>
                        </DataGrid>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

上面的代码是我想要的确切体验的精简版。如您所见,我在 DataGridTemplateColumn 中为 PropertyCollection 添加了 DataGrid。在其中放置一个 ComboBox,其中有一个 ListBoxItem,即 DataGrid。

在此设置中唯一不受欢迎的行为是 DataGrid 是一个 ComboBoxItem,这意味着当您打开 ComboBox 并单击该项目时,整个 DataGrid 将展开并且所有样式等......不再适用于我想要的方式。

那么我可以禁用 ComboBoxItem 的行为还是继承某种样式让第二个 Datagrid 显示为 ComboBox,当您单击它打开时会展开?

【问题讨论】:

    标签: c# wpf combobox datagrid


    【解决方案1】:

    以下链接可能会有所帮助,它将重定向到微软官方页面

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.datagridcomboboxcolumn?view=net-5.0

    【讨论】:

    • 感谢您的帮助!然而,这不是我要找的。简单来说,我想要组合框下拉样式的行为,但只托管一件事。所以从 ComboBox 中剥离整个 ItemSource 的东西,只使用 ComboBox 的样式
    • 或者反过来看,我可以设置一个 ComboBox 的样式来准确列出数据网格的项目,包括标题吗?喜欢继承 Datagrid 风格?
    猜你喜欢
    • 2013-06-30
    • 2014-06-12
    • 2013-12-13
    • 2017-10-29
    • 2011-01-03
    • 1970-01-01
    • 2015-10-30
    • 2013-06-11
    • 2012-02-01
    相关资源
    最近更新 更多