如何在gridview WPF中将组合框与不同的数据绑定

时间:2018-04-26 05:29:43

标签: c# wpf datagridcomboboxcolumn

我在WPF应用程序中使用combobox创建了gridview。如图所示,

    <ListView Name="MyListView" Width="261">
        <ListView.View>
            <GridView>

                <GridView.Columns>

                    <GridViewColumn DisplayMemberBinding="{Binding Operation}" />
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding Choices}" Width="100px"  />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView.Columns>

            </GridView>
        </ListView.View>
    </ListView>

而且,我已经在C#中为gridview中每行的不同数据绑定了组合框,

var dahList = new List<StatsOperation>();
dahList.Add(new StatsOperation
{
    Operation = "Op A",
    Choices = new string[] { "One", "Two", "Three" },
});
dahList.Add(new StatsOperation
{
    Operation = "Op B",
    Choices = new string[] { "4", "5", "6" },
});
this.MyListView.ItemsSource = dahList;

它没有在组合框中显示任何内容

1 个答案:

答案 0 :(得分:0)

<Window x:Class="WPFTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<ListView Name="MyListView" Width="261">
    <ListView.View>
        <GridView>

            <GridView.Columns>

                <GridViewColumn DisplayMemberBinding="{Binding Operation}" />
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Choices}" Width="100px"  />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView.Columns>

        </GridView>
    </ListView.View>
</ListView>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var dahList = new List<StatsOperation>();
            dahList.Add(new StatsOperation
            {
                Operation = "Op A",
                Choices = new string[] { "One", "Two", "Three" },
            });
            dahList.Add(new StatsOperation
            {
                Operation = "Op B",
                Choices = new string[] { "4", "5", "6" },
            });
            this.MyListView.ItemsSource = dahList;
        }
    }

    internal class StatsOperation
    {
        public string Operation { get; set; }
        public string[] Choices { get; set; }
    }
}