2

のようにC#コーディングで書けばOK

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

コレクションをその下のコントロールにバインドするには、ウィンドウのデータコンテキストを設定する必要があります。

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

わかりました、最も重要な部分を見逃したと思います。使用したい場合は、ビューのどこかにBinding設定する必要があります。これは の理論の良い出発点です。DataContextBinding

通常、これには aViewModelを使用します。例えば

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

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 に答える