WPF/Silverlight找不到StaticResource

我通过后台代码来添加的静态资源,可是在界面设计是,却提示该静态资源找不到,以下是我的代码。

code-behind:

  public partial class MainPage : UserControl
    {
        public MainPage()
        {
            Dog myDog = new Dog() { DogName="dog" };
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                Application.Current.Resources.Add("MyDog", myDog);
                App.Current.Resources.Add("MyDog", myDog);
            }
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

    }

    public class Dog
    {
        public string DogName { get; set; }
    }

XAML:
图片说明

你通过代码添加静态资源,xaml里怎么知道?
要xaml里能感知,那就老实写在xaml的里

xaml中需要先申明你的命名空间。

http://www.2cto.com/kf/201210/159356.html
http://sunxboy.iteye.com/blog/189970

XAML:


code-behind:

InitializeComponent();
Dog myDog = new Dog() { DogName = "dog" };

this.txt.SetBinding(TextBlock.TextProperty, new Binding("DogName") { Source = myDog, Mode = BindingMode.OneWay });