0
Answer

WPF DependencyProperty always null

Matt Allen

Matt Allen

9y
975
1
I'm trying to make a string property on a user control that can be defined in XAML without binding. My goal is for something like Name or any other property you can just directly enter yourself.
 
My user control, MediaStreamSource, has an ObservableCollection of my StreamResource class named Streams. I add its contents in XAML like so:
 
<local:MediaStreamSource Name="mediaSource">
<local:MediaStreamSource.Streams>
<local:StreamResource ResourceName="wildlife.wmv" ResourceLocation="includes.wildlife.wmv"/>
<local:StreamResource ResourceName="Yami_no_Prologue.mp3" ResourceLocation="includes.Yami_no_Prologue.mp3"/>
</local:MediaStreamSource.Streams>
</local:MediaStreamSource>
 
In the MediaStreamSource control, I also am registered for the CollectionChanged event on the Streams property. The following is the relevant parts of the MediaStreamSource class:
 
public class MediaStreamSource : UserControl
{
MediaStream streamer;
public MediaStreamSource()
{
Streams = new ObservableCollection<StreamResource>();
Streams.CollectionChanged += Streams_CollectionChanged;
streamer = new MediaStream();
}
private void Streams_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach (StreamResource resource in e.NewItems)
streamer.RegisterFile(resource.ResourceName, resource.ResourceLocation);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
foreach (StreamResource resource in e.OldItems)
streamer.UnregisterFile(resource.ResourceName);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
{
foreach (StreamResource resource in e.OldItems)
streamer.UnregisterFile(resource.ResourceName);
foreach (StreamResource resource in e.NewItems)
streamer.RegisterFile(resource.ResourceName, resource.ResourceLocation);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
{
streamer.UnregisterAllFiles();
}
}
public ObservableCollection<StreamResource> Streams { get; set; }
}
 
The problem is that, no matter what I seem to try, the values for both properties (ResourceName and ResourceLocation in StreamResource) are always null. It happens anywhere I check the values, including the CollectionChanged callback above where I use them.
 
Here's the StreamResource class in question:
 
public class StreamResource : UserControl
{
public static readonly DependencyProperty ResourceNameProperty =
DependencyProperty.Register("ResourceName", typeof(string), typeof(StreamResource), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public static readonly DependencyProperty ResourceLocationProperty =
DependencyProperty.Register("ResourceLocation", typeof(string), typeof(StreamResource), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string ResourceName
{
get { return (string)GetValue(ResourceNameProperty); }
set { SetValue(ResourceNameProperty, value); }
}
public string ResourceLocation
{
get { return (string)GetValue(ResourceLocationProperty); }
set { SetValue(ResourceLocationProperty, value); }
}
}
 
Any ideas? Any help would be greatly appreciated.
 
Thanks,
 
-Matt