2

如果我用 C# 编码编写它就可以了,例如

DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = true;
dg.ItemsSource = db;

分贝在哪里

public ObservableCollection<Data> db = new ObservableCollection<Data>();

db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });

它会很好地生成列和数据.. 但是如果我用 XAML 编写它就不能显示任何东西

<DataGrid ItemsSource="{Binding db}" AutoGenerateColumns="True"/>

我找不到将此集合绑定到 DataGrid 的方法。请告诉我原因

这是我所有的 xaml

<Window x:Class="testt.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>
    <StackPanel Orientation="Vertical" Name="Panel">
        <TextBlock Name="count"/>
        <DataGrid ItemsSource="{Binding db}" AutoGenerateColumns="True"/>       
    </StackPanel>
</Grid>
</Window>

谢谢你

4

6 回答 6

2

如果您不使用 MVVM,请在您的代码中尝试此操作,

DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = true;
dg.ItemsSource = db;
this.DataContext = this; //or put this line in constructor.

分贝在哪里

public ObservableCollection<Data> db = new ObservableCollection<Data>();

db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });

在 Xaml 中,

<Window x:Class="testt.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>
    <StackPanel Orientation="Vertical" Name="Panel">
        <TextBlock Name="count"/>
        <DataGrid ItemsSource="{Binding db}" AutoGenerateColumns="True"/>       
    </StackPanel>
</Grid>
</Window>
于 2013-11-12T11:00:50.077 回答
0

感谢大家的帮助,现在问题解决了!

db 应该是属性,而不是字段,所以我将一些代码更改为

public ObservableCollection<Data> db { set; get; }

我必须用这个句子设置 DataContext

this.DataContext = this;

现在它可以正常工作了:)

于 2013-11-12T12:54:31.890 回答
0

您必须实现INotifyPropertyChanged接口才能反映更改。请看下面的片段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace StylingIntroSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
    private ObservableCollection<Data> _db = new ObservableCollection<Data>();

    public ObservableCollection<Data> db {
        get { return _db; }
        set
        {
            _db = value;
            OnPropertyChanged("db");
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        LoadItems();
    }

    private void LoadItems()
    {
        db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
        db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string PropertyName)
    {
        if (PropertyChanged!= null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }
}

public class Data
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int Price { get; set; }
}

}

在 XAML 中

<Grid>
    <StackPanel Orientation="Vertical"
                Name="Panel">
        <TextBlock Name="count" />
        <DataGrid Name="MyGrid" ItemsSource="{Binding db}"
                  AutoGenerateColumns="True" />
    </StackPanel>
</Grid>
于 2013-11-12T11:33:17.980 回答
0

好吧,我想你错过了最重要的部分。如果要使用Binding,则必须在视图上设置DataContext某处。是 理论的一个良好开端Binding

通常,您会为此使用 a ViewModel。例如

public class WndViewModel : ViewModelBase // the base class implementing INotifyPropertyChanged
                                          // although you don't need it for your case 
{
    private ObservableCollection<Data> _db;
    public ObservableCollection<Data> Db 
    { 
        get { return _db ?? (_db = new ObservableCollection<Data>()); 
    }

    public WndViewModel()
    {
        Db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
        Db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });
    }
}

然后你可以使用ViewModel这样的。

<Window ...>
    <Window.DataContext>
         <someNs:WndViewModel />
    </Window.DataContext>
    <Grid>
        <StackPanel Orientation="Vertical" Name="Panel">
            <TextBlock Name="count"/>
            <DataGrid ItemsSource="{Binding Db}" AutoGenerateColumns="True"/>       
        </StackPanel>
    </Grid>
</Window> 

请注意,此代码仅用于演示目的,需要改进以获得良好的解决方案。

我还建议您熟悉该MVVM模式。你可以从阅读这篇文章开始。

于 2013-11-12T10:55:57.113 回答
0

您必须设置窗口的数据上下文以将集合与其下的控件绑定。

ObservableCollection<Data> db = new ObservableCollection<Data>();

 db.Add(new Data { Name = "person1", Description = "sssssss", Price = 15 });
 db.Add(new Data { Name = "person2", Description = "okokok", Price = 12 });

 this.DataContext = db;

现在在 XAML 中,您只需设置 itemsource 的绑定。

<DataGrid Name="grdPerson" ItemsSource="{Binding}" AutoGenerateColumns="True"></DataGrid>
于 2013-11-12T11:07:16.227 回答
0

尝试为 DataGrid 设置“DisplayMemberPath”,这是设置 DataSource 后应该做的第一件事。

于 2013-11-12T11:18:56.683 回答