Setting a custom property within a WPF/Silverlight page

后端 未结 10 1220
萌比男神i
萌比男神i 2020-12-14 15:10

This sounds like it should be simple. I have a Page declared in XAML in the normal way (i.e. with "Add new item...") and it has a custom property. I\'

10条回答
  •  执笔经年
    2020-12-14 15:36

    You could declare your element to be a element instead:

    
    
    

    That would do the trick, but you lose InitializeComponent() and the standard designer stuff. Design mode still seems to work flawlessly, though, but I haven't extensively tested this.

    UPDATE: This compiles and runs, but does not actually set MyProperty. You also lose the ability to bind event handlers in XAML (although there may be a way to restore that which I am unaware of).

    UPDATE 2: Working sample from @Fredrik Mörk which sets the property, but does not support binding event handlers in XAML:

    Code-behind:

    namespace WpfApplication1
    {
        public partial class MainWindow : Window
        {
            protected override void OnActivated(EventArgs e)
            {
                this.Title = MyProperty;
            }      
    
            public string MyProperty { get; set; }
        }
    }
    

    XAML:

     
    
    

提交回复
热议问题