2012-11-02 85 views
-1

原始問題在這裏。 Binding To A ListView更多綁定到ListView

我有固定的一個問題,現在看到的列名。但是,我無法弄清楚綁定。

從輸出窗口中的錯誤:

的XAML的
System.Windows.Data Error: 4 : Cannot find source for binding with reference 
'ElementName=This'. BindingExpression:Path=LogView.LogEntries; DataItem=null; target 
element is 'ListView' (Name='LoggingListView'); target property is 'ItemsSource' (type  'IEnumerable') 

片段與我從LogFileWindow.XAML最新嘗試。我可以發佈更多,但試圖保持的雜亂感:

<ListView Name="LoggingListView" ItemsSource="{Binding ElementName=This, Path=LogView.LogEntries} "> 
         <ListView.View> 
         <GridView> 
          <GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=Date}"></GridViewColumn> 
          <GridViewColumn Header="Time" DisplayMemberBinding="{Binding Path=Time}"></GridViewColumn> 
          <GridViewColumn Header="Event" DisplayMemberBinding="{Binding Path=Event}"></GridViewColumn> 
         </GridView> 
         </ListView.View> 
        </ListView> 

的C#視圖模型:

public class LogEntryViewModel : INotifyPropertyChanged 
{ 
    public LogEntryViewModel(LogFileEntry le) 
    { 
     _date = le.Date; 
     _time = le.Time; 
     _event = le.Event; 
    }  
    #region Members 
    private string _date; 
    public string Date 
    { 
     get { return _date; } 
     set {_date = value; 
     RaisePropertyChanged("Date");   
     } 
    } 

    private string _time; 
    public string Time 
    { 
     get { return _time; } 
     set 
     { 
      _time = value; 
      RaisePropertyChanged("Time"); 

     } 
    } 

    private string _event; 
    public string Event 
    { 
     get { return _event; } 
     set { _event = value; 
     RaisePropertyChanged("Event"); 
     } 
    } 
    private LogFileEntry _le; 
    #endregion 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 

    private void RaisePropertyChanged(string propertyName) 
    { 
     // take a copy to prevent thread issues 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

public class LogViewModel :ObservableCollection<LogEntryViewModel> 
{ 
     private ObservableCollection<LogEntryViewModel> _LogEntries; 
    public ObservableCollection<LogEntryViewModel> LogEntries = new  
    ObservableCollection<LogEntryViewModel>(); 

} 

部分類聲明和代碼隱藏在那裏我想使用它:

public partial class LogFileWindow : Window 
{ 
    public LogViewModel LogView = new LogViewModel(); 

}

+0

相同元素,你的問題是......? – Bernard

+0

爲什麼不是綁定工作。如果它工作,爲什麼我會張貼這個尋求幫助?是不是「我有一部分工作」和「我無法弄清楚綁定」足夠的線索來找出問題? –

回答

1

Visual Studio的錯誤很不言自明:你試圖做Bindi ng與ElementName並用Name =「This」搜索可視元素(假定在您的XAML可視化樹中定義)。沒有這樣的東西(顯然,我不能說,因爲你沒有發佈完整的XAML樹)。

如果你想可視元素屬性綁定到另一個屬性,你必須使用RelativeSource Self

+0

謝謝。我認爲「這是」這個關鍵字指向窗口本身。 –