2016-11-05 90 views
1

我無法正常工作。我有一個視圖),其中包含一個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)); 
    } 
} 
+0

在的ItemsSource在工具提示電網結合,首先刪除所有你把在那裏雙向/的PropertyChanged東西。如果你閱讀那些你會發現它們不相關的文檔。然後將'PresentationTraceSources.TraceLevel = High'添加到該綁定,並在運行時查看VS輸出窗格。您會在那裏看到消息,告訴您在嘗試解析綁定路徑時會發生什麼。你需要找出DataContext對那個Binding實際的對象。那會告訴你。 –

+0

我在VS輸出窗格中得到這個錯誤:System.Windows.Data錯誤:40:BindingExpression路徑錯誤:'對象'''DataGrid'(Name ='LogsGrid')'找不到'Logs'屬性。 BindingExpression:路徑=日誌; DataItem ='DataGrid'(Name ='LogsGrid');目標元素是'DataGrid'(Name ='LogsGrid');目標屬性是'ItemsSource'(類型'IEnumerable') 然後一些警告告訴我,該屬性爲空。但是這個消息在應用程序運行時顯示,而不是在向ObservableCollection添加元素時顯示。所以也許項目源不被更新? – chincheta73

+0

它告訴你Source對象上不存在Logs屬性。不是空的。不存在 - 在它正在尋找的地方。它看着錯誤的地方。改爲使用'DataContext.Logs'。 –

回答

0

我可以在你的代碼中找到的唯一不適合我的工作是在LogsGrid.ItemsSource上綁定Mode=TwoWay。這對我來說是一個例外,因爲它告訴Binding,您希望LogsGridItemsSource的新值寫回到Binding源 - 在本例中爲視圖模型的Logs屬性。這不僅僅是你想要的,但它實際上是不可能的,因爲Logs有一個私人setter(此外,DataGrid不會這樣做)。因此例外。

UpdateSourceTrigger=PropertyChanged是另外一個,也是沒有用處的,但無害此時:這告訴它寫入新Logs集合回Car.Logs。但是,DataGrid不能這樣做。它不會爲該屬性創建新值。 A TextBox將爲源屬性分配新的Text值,這就是TextBox的用途。但DataGrid不這樣做。

當我擺脫這兩件事情,它工作正常,我:

<DataGrid x:Name="LogsGrid" Grid.Row="6" ItemsSource="{Binding Logs}"> 
    <!-- etc. --> 
0

代替ItemsSource="{Binding Logs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">,使用此:ItemsSource="{Binding PlacementTarget.DataContext.Logs, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToolTip}}">

+0

還沒什麼顯示 – chincheta73

+0

@ chincheta73對不起,slz看到我更新的回答 – AnjumSKhan

+0

仍然沒有。輸出窗格這樣說:System.Windows.Data Warning:108:BindingExpression(hash = 41695345):在級別0 - 對於DataGrid.PlacementTarget找到訪問者 System.Windows.Data錯誤:40:BindingExpression路徑錯誤:'PlacementTarget'屬性在'object''DataGrid'(Name ='LogsGrid')'中找不到。 BindingExpression:路徑= PlacementTarget.DataContext.Logs; DataItem ='DataGrid'(Name ='LogsGrid');目標元素是'DataGrid'(Name ='LogsGrid');目標屬性是'ItemsSource'(類型'IEnumerable') – chincheta73