首页 > 解决方案 > 在运行时为 DataGridCell 应用模板

问题描述

我想在运行时将 DataTemplate 应用于 wpf DatagridCell。

使用下面的代码,在运行时组合框出现没有数据。

<DataTemplate x:Key="cmbTemplate">
                <ComboBox x:Name="cmbPicklist" ItemsSource="{Binding Picklist}" SelectedItem="{Binding PicklistSelectedItem}"/>
            </DataTemplate>


 <Style x:Key="MyContentStyle" TargetType="DataGridCell">          
                <Style.Triggers>
                    <DataTrigger Binding="{Binding HasPicklist}" Value="True">
                        <Setter Property="ContentTemplate" Value="{StaticResource cmbTemplate}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

在后面的代码中:

Dim oDataGridCell As DataGridCell = CType(grd_elements.Columns(0).GetCellContent(grd_elements.Items(1))?.Parent, DataGridCell)
            oDataGridCell.Style = CType(oUcDatagrid.grd_layout_main.FindResource("MyContentStyle"), Style)

我想在组合框中显示数据。谁能告诉我做错了什么以及如何纠正它以使其正常工作。

标签: wpfdatagrid

解决方案


推荐阅读