I have a FileInfo type DependencyProperty and in the PropertyChangedCallback, I can't cast the DependencyObject to FileInfo type.
public static readonly DependencyProperty TargetFileProperty =
DependencyProperty.Register("TargetFile", typeof(System.IO.FileInfo), typeof(FileSelectGroup), new PropertyMetadata(propertyChangedCallback: new PropertyChangedCallback());
private PropertyChangedCallback OnTargetFileChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var f = (System.IO.FileInfo)d; // THIS LINE GIVES ERROR BELOW
}
Error is:
Cannot convert type 'System.Windows.DependencyObject' to 'System.IO.FileInfo'
I thought maybe I was missing something obvious (I probably am) but Microsoft and this answer seem to agree I'm doing roughly the right thing.
drefers to the control where the dependency property is defined, i.e. theFileSelectGroup.You should be able to cast
e.NewValueto aSystem.IO.FileInfoto get the new value of the dependency property:Alternatively you could cast
dtoFileSelectGroupand access theTargetFileproperty of the control: