【问题标题】:WPF: Putting a DataGrid in a ComboBoxWPF:将 DataGrid 放入 ComboBox
【发布时间】:2013-02-03 17:55:25
【问题描述】:

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

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

【问题讨论】:

  • 你添加到组合框中的模型是什么样子的
  • 不确定我是否理解正确,但如果您指的是我将 ComboBox 的 ItemSource 绑定到的 ViewModel 属性,它是一个 List
  • 我只是想看看MyCumstomer 对象,所以我可以看看这是否可能,或者你有你想要创建的图片,我可以提供帮助跨度>
  • 当然。它是一个简单的 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
  • 啊,cmets 格式丢失。

标签: wpf datagrid combobox


【解决方案1】:

对于其他寻找此功能的人,我找到了一个实现 here

【讨论】:

    【解决方案2】:
    <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。现在我看不到下拉按钮或下拉菜单本身。
    • 我明白了。我们这里需要的是一些方法来修改组合框的Itemtemplate。但是如果我们把 Datagrid 放到 ItemTemplate 中,combobox 的每一项都会像 DataGrid 一样工作。我在这里尝试了一些东西,看看这是否可以管理。
    • 非常感谢。请参阅下面的答案。
    【解决方案3】:

    好的,如果我理解正确,您有一个 List&lt;Customer&gt; 列表,列表列表绑定到 ComboBox,每个子列表绑定到 DataGrid

    例子:

    Xaml:

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

    代码:

    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));
        }
    }
    

    结果:

    【讨论】:

    • 感谢您付出如此多的努力,但这确实比我需要的要多得多。正如我所说,我有一个需要绑定到 ComboBox 的 List(不是 List 列表)。普通的 ComboBox 绑定会起作用,但问题是它不显示多列。我尝试使用 Grid/TextBlocks 方法修改它的 ItemTemplate,但这看起来不太好(列对齐等问题),所以我希望用 ComboBox 替换默认的 ItemsPanel(我认为是 StackPanel)一个数据网格。
    • 我不明白如果你只有一个列表,什么会填充数据网格?还是您只想要组合中的 1 个数据网格以及其中的所有项目?
    • 我已经更新了我的答案,但是阅读您的回复我认为您不想在这里使用 DataGrid,如果您可以制作一个小样本图像来说明您正在尝试做的事情,我也许可以提供更多帮助,但我不认为以这种方式将 ComboBox 与 DataGrid 一起使用是一个好主意。
    • 好的。虽然它有点接近我想要做的,但事实是 DataGrid 应该进入组合框的“下拉面板”,因此它并不总是可见的;仅当用户单击我的组合的下拉按钮时,就像普通组合在用户单击按钮时显示项目列表一样。只是我们的 DataGrid 中有多个列,因此对于每个项目,我们将能够显示更多信息。
    • 也许Expander 控件对此会更好,但就像我说的那样,我无法完全想象你想要做什么,所以我真的无能为力:(
    猜你喜欢
    • 2021-12-29
    • 2013-06-30
    • 1970-01-01
    • 2011-09-01
    • 2022-01-11
    • 2011-06-07
    • 2011-02-28
    • 2011-05-06
    相关资源
    最近更新 更多