Reputation: 1350
I've recently started experimenting with DataBinding and implementing DependencyProperties for my custom classes. It all works fine and the possibilities are exciting, however, I came across a problem that may be only solvable by slightly modifying the overall class design. And I want to make sure this is the only option and I'm not missing anything.
So, my class stores information about video files the user imports into the application. Among other properties, it contains:
public class VideoFile {
public string FilePath { get; protected set; }
public uint ID { get; protected set; ]
public string Extension { get { return Path.GetExtension(FilePath); } }
public string FileName { get { return Path.GetFilename(FilePath); } }
}
So, I've successfully replaced FilePath with an DependencyProperty. However, in the UI, I mostly want to display just the filename, which uses some logic to provide its value. As far as I know, here are my options:
I've only met ValueConverters briefly, so I'm not sure about it. Can I use them for this purpose? Or, have I just encountered one of the main reasons they exist? :)
And last but not least, can anyone think of a situation similar to this, when a ValueConverter is not the right way to go? I want to avoid jumping straight into them, only to realize it will not work because "that one" property just can't be expressed in this way.
Upvotes: 1
Views: 675
Reputation: 16628
Don't duplicate data.
Prefer Binding
and an IValueConverter
, because that way, whenever FilePath
changes, Extension
and FileName
will be updated in the UI as well.
You could also, of course, raise PropertyChanged
for them in FilePath
's setter but that's bad practice since FilePath
should not have to care about who/what uses it.
The class then looks like:
public class VideoFile : INotifyPropertyChanged {
string m_FilePath;
public string FilePath
{
get { return m_FilePath; }
protected set
{
if(value != m_FilePath)
{
m_FilePath = value;
RaisePropertyChanged(() => this.FilePath);
}
}
}
public uint ID { get; protected set; }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged<T>(Expression<Func<T>> _PropertyExpression)
{
RaisePropertyChanged(PropertySupport.ExtractPropertyName(_PropertyExpression));
}
protected void RaisePropertyChanged(String _Prop)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(_Prop));
}
}
#endregion
}
Please note that PropertySupport
is part of Prism, but you can do without it by calling RaisePropertyChanged("FilePath")
, it's just neat to have type safety because if you change the property's name you will have a compile-time error.
Upvotes: 0
Reputation: 34200
You don't need DependencyProperties
for this. You only need a DependencyProperty
when you're going to set into a property using a MarkupExtension
, and I doubt you're doing that with a model class (because you won't be declaring this class in Xaml!).
A much more lightweight way would be to use INotifyPropertyChanged
. Here's a .NET 3.5-style implementation:
public class VideoFile : INotifyPropertyChanged
{
private string _filePath;
public string FilePath
{
get
{
return _filePath;
}
protected set
{
_filePath = value;
OnPropertyChanged("FilePath");
OnPropertyChanged("Extension");
OnPropertyChanged("FileName");
}
}
public uint ID { get; protected set; }
public string Extension { get { return Path.GetExtension(FilePath); } }
public string FileName { get { return Path.GetFileName(FilePath); } }
protected void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
(In .NET 4.5 this can be simplified somewhat thanks to the new [CallerMemberName]
attribute.)
The only downside is that you require backing fields for your properties. However, there's a VS extension called NotifyPropertyWeaver that can automate part of this work and remove the need for explicit backing properties, too.
Upvotes: 1
Reputation: 1837
As far as I understand you would like to just display File Name on UI. Then you may consider updating FileName property whenever FilePath dependency property is changed (OnChangedFilePath method). You can also check if FilePath is OK in ValidateFilePath method. Please note that FileName must be also a dependency property or supporting IPropertyChanged, otherwise UI will not be updated when you change it. You do not need to use a converter for this purpose.
public string FilePath
{
get { return (string)GetValue(FilePathProperty); }
set { SetValue(FilePathProperty, value); }
}
private static object CoerceFilePath(DependencyObject d, object value)
{
return value;
}
private static bool ValidateFilePath(object Value)
{
return true;
}
private static void OnChangedFilePath(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public static readonly DependencyProperty FilePathProperty =
DependencyProperty.Register("FilePath", typeof(string), typeof(ClassName),
new PropertyMetadata(@"C:\File.avi", OnChangedFilePath, CoerceFilePath),
new ValidateValueCallback(ClassName.ValidateFilePath));
Upvotes: 0