2009-10-14 59 views
1

我無法讓我的綁定在Detail ListView上工作。我在下面粘貼了我所有的MVVM模式代碼。請幫忙!!!主要細節MVVM WPF不工作

筆者認爲: DirectoryDe​​tailView.cs

<UserControl x:Class="S2.Views.DirectoryDetailView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions> 
    <ListView Grid.Column="0" ItemsSource="{Binding Path = DirectoryDetails}" 
       IsSynchronizedWithCurrentItem="True" 
       SelectedItem="{Binding SelectedDirName, Mode=TwoWay}"> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn DisplayMemberBinding="{Binding Path = FileName}" 
           Header="File Name"/> 
      </GridView> 
     </ListView.View> 
    </ListView> 
    <ListView Grid.Column="1" Margin="10,0,0,0" ItemsSource="{Binding Path = DirectoryDetails}"> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn DisplayMemberBinding="{Binding Path = FileDetails.Length}" 
           Header="Length"/> 
       <GridViewColumn DisplayMemberBinding="{Binding Path = FileDetails.LastAccessTime}" 
           Header="LastAccessTime"/> 
      </GridView> 
     </ListView.View> 
    </ListView> 
</Grid> 

我的模型:

public class DirectoryModel : INotifyPropertyChanged 
{ 
    private string _fileName; 
    private DateTime _createdTime; 

    public string FileName 
    { 
     get 
     { 
      return _fileName; 
     } 
     set 
     { 
      _fileName = value; 
      RaisePropertyChanged("FileName"); 
     } 
    } 

    private IEnumerable<FileDetails> _fileDetails; 

    public IEnumerable<FileDetails> FileDetails 
    { 
     get 
     { 
      return _fileDetails; 
     } 
     set 
     { 
      _fileDetails = value; 
      RaisePropertyChanged("FileDetails"); 
     } 

    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 

    protected void RaisePropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler propertyChanged = PropertyChanged; 

     if (propertyChanged != null) 
     { 
      propertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

public class FileDetails 
{ 
    public long Length { get; set; } 

    public DateTime LastAccessTime { get; set; } 
} 

我的視圖模型:

public class DirectoryViewModel : BaseViewModel 
{ 
    private IEnumerable<DirectoryModel> _directoryDetails; 

    public IEnumerable<DirectoryModel> DirectoryDetails 
    { 
     get 
     { 
      var service = GetService<IDirectoryService>(); 
      _directoryDetails = service.GetDirectoryDetails(); 
      return _directoryDetails; 
     } 
     set 
     { 
      if(_directoryDetails != value) 
      { 
       _directoryDetails = value; 
       base.RaisePropertyChanged("DirectoryDetails"); 
      } 
     } 
    } 

    private DirectoryModel _selectedDirName; 

    public DirectoryModel SelectedDirName 
    { 
     get 
     { 
      return _selectedDirName; 
     } 
     set 
     { 
      _selectedDirName = value; 
      base.RaisePropertyChanged("SelectedDirName"); 
     } 
    } 
} 

請讓我知道,W我做錯了什麼?

感謝, AG

+0

你的問題到底是什麼? – 2009-10-14 13:09:24

+0

您忘了粘貼可能包含您遇到問題的綁定的XAML? – 2009-10-14 13:43:44

+0

我粘貼了我的Xaml代碼,但它不出現在主板上。不知道爲什麼?我試圖編輯帖子幾次仍然無法設法顯示Xaml。你知道可能是什麼問題。 – netmatrix01 2009-10-14 14:20:21

回答

0

硬盤沒有看到XAML說,但我的第一個想法是:1)你有沒有DataContext屬性設置爲視圖模型或2)你有一些語法問題,在結合自身。

您應該使用ObservableCollection而不是IEnumerable < DirectoryModel>來支持DataBinding。我也不確定你的DirectoryDe​​tails getter的實現是否有益。你的setter直接設置私有變量並觸發PropertyChanged事件 - 這是正確的。但是你的getter也直接設置變量,繞過PropertyChanged事件。更何況,你有一個吸氣劑做一個二傳手的工作,這在幾個層面上可能是一個壞主意。我想你會更好地簡化你的getter並讓它只返回私有變量。你真的需要每次都檢索細節,或者你可以使用局部變量嗎?

我也指出你不需要在你的模型上實現INotifyPropertyChanged:ViewModel需要這個接口來支持DataBinding,但是將它添加到Model類沒有什麼實際價值。

+0

我現在設法顯示我的xaml,請你幫忙! – netmatrix01 2009-10-16 11:27:29

+0

我不知道你是否仍然需要幫助,但正如我上面提到的,我沒有看到你設置DataContext的位置。你DirectoryViewModel對象需要被定義爲一個數據源,然後實例化和地方引用: 添加引用您的命名空間: 的xmlns:YourNamespace =「CLR的命名空間:YourNamespace;裝配= YourAssembly」 在你的資源: 然後添加數據源作爲您的DataContext: 2009-10-29 19:46:10

+0

對不起 - DataContext應該是這樣的:DataContext =「{綁定源= {StaticResource myDataSource}}」 – 2009-10-29 19:48:35

9

我不記得我來自哪裏得到這個技術,但它的真正有用的用於調試的綁定時

類添加到一個名爲Debugconverter

public class DebugConverter : IValueConverter { 
    public object Convert(object value, 
    Type targetType, object parameter, 
    System.Globalization.CultureInfo culture) { 

    return value; //set the breakpoint here 
    } 

    public object ConvertBack(object value, 
    Type targetType, 
    object parameter, 
    System.Globalization.CultureInfo culture) { 

    return value; 
    } 

}

然後我添加一個項目參考它在app.xaml

 <currentProjectNamespace:DebugConverter 
     x:Key="debugConverter" /> 

然後用它在一個綁定,

Binding="{Binding Path=PropertyName, Converter={StaticResource debugConverter}}" 

當綁定發生時,你會得到一個斷點命中,我會被搞砸了。同時檢查輸出窗口,發現綁定失敗。

+0

好主意。我即將需要做一些嚴重的數據綁定控制,這(調試數據綁定)是我不知道如何解決的事情之一。我用來調試綁定的技巧始終是設置「Path =。」的 – 2009-10-15 02:48:47

+0

。在我的綁定中,看看它試圖綁定什麼,通常它不是你期望的對象。 – 2009-10-15 02:55:51

0

將是有益的知道是什麼問題...從來沒有少,我要問的問題是: -

是你的干將被擊中(把斷點他們)?

是第一個列表視圖工作,第二個不是?

如果它只是第二個失敗,我會猜測問題是你試圖將兩列綁定到屬性IEnumerable FileDetails,然後你試圖按照IEnumerable屬性路徑,這將不工作,因爲它是一組對象,而不是一個。你有沒有從上面的列表視圖複製和粘貼你的代碼,而不是正確設置項目源?

運行時輸出窗口是什麼?它通常會告訴您無法找到綁定路徑。如果你按照上面的調試轉換器建議,你可以找出你綁定的東西(如果有的話)