在datagrid wpf中绑定组合框的动态数据

时间:2013-03-08 16:30:08

标签: c# wpf

<Window x:Class="Practise.DropdownGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:staticData="clr-namsespace:ComboBoxDGWPF"
        Title="DropdownGrid" Height="700" Width="600" WindowState="Maximized" Background="#f0f0f0">
    <Window.Resources>
        <staticData:StatusList x:Key="StatusList" />  // Error of Namespace
    </Window.Resources>

    <StackPanel VerticalAlignment="Center" Width="300">

        <DataGrid AutoGenerateColumns="False" Height="366" Name="DemoGrid" ItemsSource="{Binding objLabel}" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Subject}" Header="Demo Label" Width="*">

                </DataGridTextColumn>
                <DataGridTemplateColumn Header="Drop Down">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox Name="ComboBoxName" ItemsSource="StatusList" SelectedItem="Status"  >                                

                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>

* 代码*

namespace Practise
{
    /// <summary>
    /// Interaction logic for DropdownGrid.xaml
    /// </summary>
    public partial class DropdownGrid : Window
    {
        public DropdownGrid()
        {
            InitializeComponent();
            List<MainContent> objMainContent = new List<MainContent>{
                new MainContent { Subject = "Maths", Status = "Open" },
                new MainContent { Subject = "Science", Status = "Closed" },
                new MainContent { Subject = "Computers" , Status = "high"}
            };

            DemoGrid.ItemsSource = objMainContent;
        }
        public class MainContent
        {
            public string Subject { get; set; }
            public string Status { get; set; }
        }
        public class StatusList : List<string>
        {
            public StatusList()
            {
                this.Add("Open");
                this.Add("Closed");
                this.Add("High");
                this.Add("Low");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您不能在Xaml中使用嵌套类,StatusList必须是自己的类,而不是嵌套在DropdownGrid

namespace Practise
{
    /// <summary>
    /// Interaction logic for DropdownGrid.xaml
    /// </summary>
    public partial class DropdownGrid : Window
    {
        public DropdownGrid()
        {
            InitializeComponent();
            List<MainContent> objMainContent = new List<MainContent>{
                new MainContent { Subject = "Maths", Status = "Open" },
                new MainContent { Subject = "Science", Status = "Closed" },
                new MainContent { Subject = "Computers" , Status = "high"}
            };

            DemoGrid.ItemsSource = objMainContent; 
        }
    }

    public class MainContent
    {
        public string Subject { get; set; }
        public string Status { get; set; }
    }

    public class StatusList : List<string>
    {
        public StatusList()
        {
            this.Add("Open");
            this.Add("Closed");
            this.Add("High");
            this.Add("Low");
        }
    }
}

<Window x:Class="Practise.DropdownGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:staticData="clr-namsespace:Practise"
        Title="DropdownGrid" Height="700" Width="600" WindowState="Maximized" Background="#f0f0f0">
    <Window.Resources>
        <staticData:StatusList x:Key="StatusList" />  // Error of Namespace
    </Window.Resources>

    <StackPanel VerticalAlignment="Center" Width="300">

        <DataGrid AutoGenerateColumns="False" Height="366" Name="DemoGrid" ItemsSource="{Binding objLabel}" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Subject}" Header="Demo Label" Width="*">

                </DataGridTextColumn>
                <DataGridTemplateColumn Header="Drop Down">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox Name="ComboBoxName" ItemsSource="{StaticResource StatusList}" SelectedItem="{Binding Status}"  >                                

                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>