开发者

Access UI controls between partial classes

In my XAML I have a Button, with a button click event. I initialize the component in my MainWindow class : RibbonWindow. I have a the Button Click function in a separate class, but error window gives me "FooApplication.MainWindow" does not contain a definition for fooBtn_Click", below is my code

namespace FooApplication1 {
    public partial class MainWindow : RibbonWindow
    {
        public MainWindow()
        {
            InitializeComponent();

        }
    }


/* in another .cs class file located in the same project */
    namespace FooApplication1
    {
        public partial class Jenny : MainWindow
        { 开发者_C百科       
            public void btnApplyL_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Hello");
            }
        }    
    }

I tried to give Jenny a RibbonWindow partial, thinking it might be the issue, but gives error. What is wrong?


Your XAML file needs to point to the class responsible for the event handling via the class attribute.

x:Class="FooApplication1.Jenny"

Defining event handlers within the XAML forces the use of the class attribute as it will dictate where the handlers exist. You can still have the partial classes; it's just that the XAML needs to know which class is responsible for the event handling which modifying the class attribute will address.

In addition you will need to move the InitializeComponent(); call from the MainWindow class to the Jenny class.

    public partial class Jenny : MainWindow
    {        
        public Jenny()
        {
           InitializeComponent();
        }

        ...
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜