WPF:将 DataGrid 放入 ComboBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14950925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:36:53  来源:igfitidea点击:

WPF: Putting a DataGrid in a ComboBox

wpfdatagridcombobox

提问by dotNET

In WPF, how can I put a DataGrid in a ComboBox to show multiple columns? Stuff like the following doesn't seem to do anything:

在 WPF 中,如何将 DataGrid 放在 ComboBox 中以显示多列?像下面这样的东西似乎没有任何作用:

<ComboBox>
    <ItemsPanelTemplate>
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding customerName}" />                 
                <DataGridTextColumn Binding="{Binding billingAddress}" />
            </DataGrid.Columns>
        </DataGrid>
    </ItemsPanelTemplate>
</ComboBox>

采纳答案by dotNET

For anyone else looking for this, I found one implementation here.

对于其他寻找此内容的人,我在这里找到了一个实现。

回答by sa_ddam213

Ok, if I understand correctly you have a List of List<Customer>, the List of lists is bound to the ComboBox and each of the child lists is bound to the DataGrid

好的,如果我理解正确的话,你有一个 List of List<Customer>,List of List 绑定到 ComboBox 并且每个子列表绑定到 DataGrid

Example:

例子:

Xaml:

Xml:

<Window x:Class="WpfApplication13.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">

    <Grid DataContext="{Binding ElementName=UI}">
        <ComboBox DataContext="{Binding ComboItems}" Height="27" VerticalAlignment="Top" >
            <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" ColumnWidth="150" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" Binding="{Binding CustomerName}" />
                    <DataGridTextColumn Header="Address" Binding="{Binding BillingAddress}" />
                </DataGrid.Columns>
            </DataGrid>
        </ComboBox>
    </Grid>
</Window>

Code:

代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{

    private ObservableCollection<Customer> _comboItems = new ObservableCollection<Customer>();

    public MainWindow()
    {
        InitializeComponent();
        ComboItems.Add(new Customer { CustomerName = "Steve", BillingAddress = "Address" });
        ComboItems.Add(new Customer { CustomerName = "James", BillingAddress = "Address" });
    }

    public ObservableCollection<Customer> ComboItems
    {
        get { return _comboItems; }
        set { _comboItems = value; }
    }
}


public class Customer : INotifyPropertyChanged
{
    private string _customerName;
    private string _billingAddress;

    public string CustomerName
    {
        get { return _customerName; }
        set { _customerName = value; RaisePropertyChanged("CustomerName"); }
    }

    public string BillingAddress 
    {
        get { return _billingAddress; }
        set { _billingAddress = value; RaisePropertyChanged("BillingAddress"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Result:

结果:

enter image description here

在此处输入图片说明

回答by Manish

<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>