WPF DependencyProperty始终为null

时间:2014-11-14 16:02:00

标签: c# wpf xaml c#-4.0

我正在尝试在用户控件上创建一个字符串属性,该属性可以在XAML中定义而不进行绑定。我的目标是像Name或任何其他属性,你可以直接输入自己。

我的用户控件MediaStreamSource有一个名为Streams的StreamResource类的ObservableCollection。我将其内容添加到XAML中,如下所示:

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

在MediaStreamSource控件中,我还在Streams属性上注册了CollectionChanged事件。以下是MediaStreamSource类的相关部分:

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

问题在于,无论我多么尝试,两个属性(StreamResource中的ResourceName和ResourceLocation)的值始终为null。它发生在我检查值的任何地方,包括上面我使用它们的CollectionChanged回调。

这是有问题的StreamResource类:

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

我也尝试过INotifyPropertyChanged,但这不起作用,并且已被确认为错误的方法。

更新:如果我执行以下操作,它可以正常工作。对于通过XAML添加的项目,它仅为null。

mediaSource.Streams.Add(new StreamResource()
{
    ResourceName = "Test.wmv",
    ResourceLocation = "includes.Wildlife.wmv"
});

有什么想法吗?任何帮助将不胜感激。

谢谢,

-Matt

0 个答案:

没有答案