开发者

How to use inheriting in WPF

开发者 https://www.devze.com 2022-12-10 11:34 出处:网络
I try to use inheriting in WPF. I have asked a question about this earlier, but no one answered correct. So I try to make a BaseWindo开发者_如何学编程w class with some UI elements and I want that othe

I try to use inheriting in WPF. I have asked a question about this earlier, but no one answered correct. So I try to make a BaseWindo开发者_如何学编程w class with some UI elements and I want that other windows, that inherit my BaseWindow would have these UI elements. How to do that. My practise with WinForms applications dont work anymore. Maybe there are some simple examples or smth..? Thanks


If you want to inherit from a window, you need to create an implementation of a window in code only, and you can then inherit from this - note that your Window declaration in the XAML would need to change to point to this code, e.g.

<src:BaseWindow xmlns:src="clr-namespace:BaseWindowNamespace" ...>

If you define the base window using XAML, you'll get the following errors:

"'WpfApplication1.Window1.InitializeComponent()' hides inherited member BaseWindowNamespace.BaseWindow.InitializeComponent()'. Use the new keyword if hiding was intended"

"BaseWindowNamespace.BaseWindow cannot be the root of a XAML file because it was defined using XAML"

Now, at this point, I must point out that this is counter to the way that you should handle composition of your window. The "standard" way of doing this would be to use content templates to display user controls which could be swapped and styled to achieve different looks and functionality. In this way, the function of the window becomes that of a harness, and you can achieve a clean separation of content which can be easily implemented using MVVM.


Did you try the BasedOn tag? I don't know about windows, but that's what you use for UserControls AFAIK.

0

精彩评论

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