我無法正常工作。我有一個視圖),其中包含一個DataGrid,其中包含可觀察集合(MyDataCollection)的項目。 MyDataCollection的每一項都有不同的屬性(名稱,描述,...,日誌)。日誌是Log項目的可觀察集合本身。每個日誌項目都有不同的屬性(日期,人員...)。綁定到DataGrid行中的嵌套屬性工具提示WPF
我的數據網格填充有MyDataCollection項目,每行設置一個工具提示。就像這樣:
<DataGrid ItemsSource="{Binding MyDataCollection}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="ToolTip">
<Setter.Value>
<Border>
<Grid Margin="5" MaxWidth="400">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
...
</Grid.RowDefinitions>
...
<DataGrid x:Name="LogsGrid" Grid.Row="6" ItemsSource="{Binding PlacementTarget.DataContext.Logs, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToolTip}}">
<DataGrid.Columns>
<DataGridTextColumn Header="Date"
Binding="{Binding Date}"
/>
<DataGridTextColumn Header="Person"
Binding="{Binding Person.FullName}"
/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Border>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
</DataGrid>
我可以看到提示,我可以看到數據網格與頭「日期」和「人」,但格內容爲空的提示。看起來綁定設置不正確。任何人都可以幫我一把嗎?謝謝
更新1: MyDataColletion包含我的自定義類「汽車」的對象。轎車在這裏的定義是:
public class Car : INotifyPropertyChanged
{
public string name;
public string description;
public Contact assignedTo;
public ObservableCollection<Log> logs = new ObservableCollection<Log>();
public string Name
{
get
{
return this.name;
}
set
{
if (this.name != value)
{
this.name = value;
NotifyPropertyChanged("Name");
}
}
}
public string Description
{
get
{
return this.description;
}
set
{
if (this.description != value)
{
this.description = value;
NotifyPropertyChanged("Description");
}
}
}
public Contact AssignedTo
{
get
{
return this.assignedTo;
}
set
{
if (this.assignedTo != value)
{
this.assignedTo = value;
NotifyPropertyChanged("AssignedTo");
}
}
}
public ObservableCollection<Log> Logs
{
get
{
return this.logs;
}
private set //TODO : Check if this is correct
{
if (this.logs != value)
{
this.logs = value;
NotifyPropertyChanged("Logs");
}
}
}
public Car()
{
// TODO: Delete this: (only here for testing)
Contact c = new Contact();
c.Name = "Test";
c.LastName = "Test";
for (int i = 0; i < 4; i++)
AddLog(DateTime.Now, c, new TimeSpan(2, 0, 0));
}
public void AddLog(DateTime date, Contact person, TimeSpan time)
{
Log newLog = new Log(date, person, time);
Logs.Add(newLog);
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
我的日誌類是:
public class Log : INotifyPropertyChanged
{
DateTime date;
Contact person;
TimeSpan time;
public DateTime Date
{
get
{
return this.date;
}
set
{
if (this.date != value)
{
this.date = value;
NotifyPropertyChanged("Date");
}
}
}
public Contact Person
{
get
{
return this.person;
}
set
{
if (this.person != value)
{
this.person = value;
NotifyPropertyChanged("Person");
}
}
}
public TimeSpan Time
{
get
{
return this.time;
}
set
{
if (this.time != value)
{
this.time = value;
NotifyPropertyChanged("Time");
}
}
}
public Log(DateTime date, Contact person, TimeSpan time)
{
this.date = date;
this.person = person;
this.time = time;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
在的ItemsSource在工具提示電網結合,首先刪除所有你把在那裏雙向/的PropertyChanged東西。如果你閱讀那些你會發現它們不相關的文檔。然後將'PresentationTraceSources.TraceLevel = High'添加到該綁定,並在運行時查看VS輸出窗格。您會在那裏看到消息,告訴您在嘗試解析綁定路徑時會發生什麼。你需要找出DataContext對那個Binding實際的對象。那會告訴你。 –
我在VS輸出窗格中得到這個錯誤:System.Windows.Data錯誤:40:BindingExpression路徑錯誤:'對象'''DataGrid'(Name ='LogsGrid')'找不到'Logs'屬性。 BindingExpression:路徑=日誌; DataItem ='DataGrid'(Name ='LogsGrid');目標元素是'DataGrid'(Name ='LogsGrid');目標屬性是'ItemsSource'(類型'IEnumerable') 然後一些警告告訴我,該屬性爲空。但是這個消息在應用程序運行時顯示,而不是在向ObservableCollection添加元素時顯示。所以也許項目源不被更新? – chincheta73
它告訴你Source對象上不存在Logs屬性。不是空的。不存在 - 在它正在尋找的地方。它看着錯誤的地方。改爲使用'DataContext.Logs'。 –