Hard to say without seeing the XAML, but my first thoughts are either 1) you have not set the DataContext property to the ViewModel or 2) you have some syntax issue in the Binding itself.
Up vote 1 down vote favorite 1 share g+ share fb share tw.
I am unable to get my bindings working on the Detail ListView. I have pasted all my MVVM pattern code below. Please help!
My View : DirectoryDetailView. Cs 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; public IEnumerable 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 _directoryDetails; public IEnumerable DirectoryDetails { get { var service = GetService(); _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 am I doing wrong? Thanks, AG c# wpf data-binding mvvm link|improve this question edited Aug 19 '11 at 3:51Dave Clemmer1,77331030 asked Oct 14 '09 at 12:55netmatrix011509 83% accept rate.
– Pieter Breed Oct 14 '09 at 13:43 I have pasted my Xaml code but it doesn't appears on the board. Don't know why?. I have tried to edit the post several time still cannot manage to display Xaml.
Do you know what could be the issue. – netmatrix01 Oct 14 '09 at 14:20 The issue relates the XML brackets (I think)... maybe a bug in SO code... I had issues with XML code today too – Pieter Breed Oct 14 '09 at 15:18 I have managed to get my xaml showing now, was wondering if anyone can help – netmatrix01 Oct 16 '09 at 11:26.
Hard to say without seeing the XAML, but my first thoughts are either 1) you have not set the DataContext property to the ViewModel or 2) you have some syntax issue in the Binding itself. You should use ObservableCollection instead of IEnumerable to support DataBinding. I'm also not sure the implementation of your DirectoryDetails getter is beneficial.
Your setter sets the private variable directly and fires the PropertyChanged event - this is proper. But your getter also sets the variable directly, bypassing the PropertyChanged event. Not to mention that you have a getter doing the work of a setter, which is probably a bad idea on several levels.
I think you would be better to simplify your getter and have it just return the private variable. Do you really need to retrieve teh details everytime or can you use the local variable? I would also point out that you do not need to implement INotifyPropertyChanged on your Model: the ViewModel needs this interface to support DataBinding, but there is no real value in adding it to the Model class.
I have managed to show my xaml now, can you please help! – netmatrix01 Oct 16 '09 at 11:27 I don't know if you still need help or not, but as I mentioned above, I do not see where you set the DataContext. Your DirectoryViewModel object needs to be defined as a DataSource, and then instantiated and referenced somewhere: Add a reference to your Namespace: xmlns:YourNamespace="clr-namespace:YourNamespace;assembly=YourAssembly" In your Resources: Then add the data source as your DataContext: – Joel Cochran Oct 29 '09 at 19:46 Sorry - DataContext should be like this: DataContext="{Binding Source={StaticResource myDataSource}}" – Joel Cochran Oct 29 '09 at 19:48.
I cant remember where I got this technique from but its really useful when used to debug bindings add a class to the project called 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; } } then I add a reference to it in app.
Xaml then use it in a binding, Binding="{Binding Path=PropertyName, Converter={StaticResource debugConverter}}" when the binding happens you get a breakpoint hit, I would be screwed without it. Also check the output window, there is alist of binding failures there.
Great idea. I am about to need to do some heavily databound controls and this (debugging databinding) was one of the things I was not sure how to address. – Anthony Potts Oct 15 '09 at 2:48 on of the tricks I use to debug bindings all the time is to set "Path=.
" in my binding to see what it is trying to bind to, often its not the object you expect. – Aran Mulholland Oct 15 '09 at 2:55.
If it is only the second that is failing I would guess that the problem is that you are trying to bind two columns to the property IEnumerable FileDetails, and then you are trying to follow a property path on a IEnumerable, which will not work, as it is a group of objects, not one. Have you copied and pasted your code from the listview above and not set the items source correctly? What is in your output window when you run?
It usually tells you that a binding path could not be found. If you follow the debug converter suggestion above, you can find out what you are binding to (if anything).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.