WPF:如何在ComboBox中放置一个DataGrid

6
在WPF中,我如何把一个DataGrid放在ComboBox里以显示多列?像以下代码似乎没有任何作用:
<ComboBox>
    <ItemsPanelTemplate>
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding customerName}" />                 
                <DataGridTextColumn Binding="{Binding billingAddress}" />
            </DataGrid.Columns>
        </DataGrid>
    </ItemsPanelTemplate>
</ComboBox>

你要添加到组合框中的模型是什么样子的? - sa_ddam213
不确定我是否理解您的意思,但如果您指的是我将我的ComboBox的ItemSource绑定到的ViewModel属性,它是一个List <Of MyCumstomerBO>。 - dotNET
我只是想看一下 MyCustomer 对象,以便确定这是否可行,或者你有关于你正在尝试创建的内容的图片吗?也许我能提供帮助。 - sa_ddam213
当然。它是一个简单的C#类,就像这样:Class CustomerBO Public Property customerID As String Public Property customerName As String Public Property billingAddress As String Public Property billingCity As String ... End Class - dotNET
啊,注释丢失了格式。 - dotNET
显示剩余4条评论
2个回答

0

对于其他寻找此类解决方案的人,我在这里找到了一个实现链接


0
<ComboBox Width="150" Height="30" Name="cb">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <DataGridRow DataContext="{Binding}" Height="30" Width="150">
                        <DataGridRow.Template>
                            <ControlTemplate>
                                <Grid> 
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition Width="5"/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding customerName}" Margin="2"></TextBlock>
                                    <Border  BorderBrush="Black" BorderThickness="1" Grid.Column="1" Margin="2"></Border>
                                    <TextBlock Grid.Column="2" Text="{Binding billingAddress}" Margin="2"></TextBlock>
                                </Grid>

                            </ControlTemplate>
                        </DataGridRow.Template>

                    </DataGridRow>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

这会导致ComboBox完全失效。现在我既看不到下拉按钮,也看不到下拉本身。 - dotNET
我理解了。我们需要的是修改Combobox的ItemTemplate的某种方法。但是,如果我们将DataGrid直接放到ItemTemplate中,那么每个Combobox选项都会像一个DataGrid一样工作。我在这里尝试了一些东西,看看是否可行。 - Manish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接