使用DataTable wpf填充Datagrid

时间:2015-05-20 18:55:44

标签: c# wpf datagrid

我正在尝试填充DataGrid。但是我得到了类型&System; Windows.Warkows.Markup.XamlParseException'发生在PresentationFramework.dll

这是我的代码:

public partial class Course : Window
{
    public Course()
    {
        InitializeComponent();
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Col1", Type.GetType("System.String")));
        dt.Columns.Add(new DataColumn("Col2", Type.GetType("System.String")));
        dt.Columns.Add(new DataColumn("ButtonsList", Type.GetType("SchoolDesktopClient.ButtonsList")));

        dt.Rows.Add("Test1", "Test1", new ButtonsList { Content = "Test1", ToolTip = "Test1" });
        dt.Rows.Add("Test2", "Test2", new ButtonsList { Content = "Test2", ToolTip = "Test2" });

        dtGrid.ItemsSource = dt.DefaultView;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Course course = new Course();
        course.Show();

    }




public class ButtonsList
{
    public String Content { get; set; }
    public string ToolTip { get; set; }
}

Xaml here:

1 个答案:

答案 0 :(得分:1)

首先,您应该考虑使用至少具有一点MVVM模式的WPF。 这意味着您将为View(V)提供名为ViewModel(VM)的DataContext类。我们将把以后绑定控件的所有数据移到那里。

<强>视图模型:

public class ViewModel
        {
            public DataTable dt { get; set; }

            public ViewModel()
            {
                dt = new DataTable();
                dt.Columns.Add(new DataColumn("Col1", typeof(String)));
                dt.Columns.Add(new DataColumn("Col2", typeof(String)));
                dt.Columns.Add(new DataColumn("ButtonsList", typeof(ButtonsList)));

                dt.Rows.Add("Test1", "Test1", new ButtonsList { Content = "ButtonTest1", ToolTip = "TooltipTest1" });
                dt.Rows.Add("Test2", "Test2", new ButtonsList { Content = "ButtonTest2", ToolTip = "TooltipTest2" });
            }
        }

我们的窗口(View)现在只负责它的用途:呈现数据。我已将名称更改为基本,生成一个。希望它不会让你感到困惑。

查看:

public partial class MainWindow : Window
    {
        ViewModel _viewModel;

        public MainWindow()
        {
            _viewModel = new ViewModel();
            this.DataContext = _viewModel;

            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainWindow MainWindow = new MainWindow();
            MainWindow.Show();

        }
    }

我现在将直接绑定到ViewModel中属性公开的数据。在此示例中,只有外部显示的属性为DataTable dt

查看(XAML):

<Window x:Class="Course.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">
    <Grid>
        <DataGrid x:Name="dtGrid" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="-17,-6.538" Height="299" Width="497" ItemsSource="{Binding dt}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Col1" Binding="{Binding Path=Col1}"></DataGridTextColumn>
                <DataGridTextColumn Header="Col2" Binding="{Binding Path=Col2}"></DataGridTextColumn>
                <DataGridTemplateColumn Header="Button">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="{Binding Path=ButtonsList.Content}" ToolTip="{Binding Path=ButtonsList.Tooltip}" Click="Button_Click"></Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>

        </DataGrid>


    </Grid>
</Window>

让我们将ButtonsList分成不同的类,以实现可重用性和访问目的。

<强> ButtonsList:

public class ButtonsList
        {
            public String Content { get; set; }
            public string ToolTip { get; set; }
        }

现在所有人都应该很好地工作并满足基本的编程模式要求。