WPF MVVM Wizard not working

I cannot get my bindings to work in the Detail ListView. I have inserted all my MVVM templates below. Please, help!!!

My view: DirectoryDetailView.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> 

My model:

 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; } } 

My ViewModel:

 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"); } } } 

Please let me know what I am doing wrong?

Thanks, AG

+1
c # data-binding wpf mvvm
source share
3 answers

It's hard to say without seeing XAML, but my first thoughts are: either 1) you did not set the DataContext property in the ViewModel or 2) you have a syntax problem in Binding itself.

You should use ObservableCollection instead of IEnumerable <DirectoryModel> to support DataBinding. I'm also not sure if implementing your GetDataData Getter is beneficial. Your setter directly sets the private variable and fires the PropertyChanged event - thatโ€™s right. But your getter also sets the variable directly, bypassing the PropertyChanged event. Not to mention that you have a getter that does the work of the setter, which is probably a bad idea at several levels. I think you would be better off simplifying your getter and just returning the private variable. Do you really need to get data every time, or can you use a local variable?

I would also like to point out that you do not need to implement INotifyPropertyChanged in your model: ViewModel needs this interface to support DataBinding, but there is no real value when adding it to the Model class.

0
source share

I canโ€™t remember where I got this technique from, but itโ€™s very useful when used to debug bindings

add class to debugconverter project

 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; } 

}

then I will add a link to it in app.xaml

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

then use it in binding

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

when the binding occurs, you get a break point hit, I would have screwed up without it. Also check the output window, there is a list of binding failures.

+9
source share

it would be useful to know what the problem is ... however, the questions that I would ask: -

do your recipients fall (put a breakpoint on them)?

- The first view of the list, and the second - not?

If this is only the second error, I would suggest that the problem is that you are trying to bind two columns with the IEnumerable FileDetails attribute, and then you are trying to follow the property path in IEnumerable, which will not work, as this is a group of objects, not one. Have you copied your code and pasted it from the list above, but didnโ€™t set the source of the elements correctly?

what is in your output window at startup? he usually tells you that the binding path was not found. if you follow the debug converter suggestion above, you can find out what you are getting attached for (if any)

0
source share

All Articles