0

如何在 DataGrid 绑定中指定对象?我的数据网格有ItemsSource="{Binding Source=TripsCollection}"andAutoGenerateColumns="False"和 Columns 设置为:

<DataGrid.Columns>
     <DataGridTextColumn Header="ID" Binding="{Binding TripID}" IsReadOnly="True"></DataGridTextColumn>
     ...
</DataGrid.Columns>

但是有错误:

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“Char”(HashCode=7209070)上找不到“TripID”属性。绑定表达式:路径=TripID;DataItem='Char' (HashCode=7209070); 目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”)。

如何将对象设置为 Trip 对象?

数据上下文类:

private ObservableCollection<Trip> _tripsCollection;
    public ObservableCollection<Trip> TripsCollection
    {
        get { return _tripsCollection; }
        set { _tripsCollection = value; NotifyPropertyChanged("TripsCollection"); }
    }

旅行等级:

[Table(Name="Trips")]
public class Trip
{
    private uint _tripID;
    private string _start;
    private string _end;
    private DateTime _date;
    private double _length;
    private string _note;
    private uint _carID = 0;
    private EntityRef<Car> _Car;

    ..and properties for this fields..
}

设置数据上下文

public MainWindow()
{
    InitializeComponent();
    string connString = @"Data Source=.\SQLEXPRESS; Integrated security=SSPI; Connect Timeout=30"; // User Instance=True";
    conn = new SqlConnection(connString);
    conn.Open();

    LogBookViewModel lbvm = new LogBookViewModel(conn);
    DataContext = lbvm;
}
4

1 回答 1

1

您必须更改 ItemsSource 中的绑定,如下所示:

ItemsSource="{Binding Source=TripsCollection}"

对此:

ItemsSource="{Binding TripsCollection}"

如果您将来遇到绑定问题,请尝试使用snoop

于 2012-09-30T15:31:30.513 回答