开发者

Can a WPF base class contain a control for a derived class?

开发者 https://www.devze.com 2022-12-24 09:40 出处:网络
I have several UserControls that have some of the same controls doing the same job. Is it possible to extract those controls into a base class?

I have several UserControls that have some of the same controls doing the same job.

Is it possible to extract those controls into a base class?

When I have tried it, I get an error that the definition in the generated .g.cs file will hide the parent def.

What I would like to do:

public class ctlBase : UserControl
{
  internal CheckBox chkBox { get; set; }
}
In the .xaml of the derived class:  
<Gr开发者_StackOverflow社区id>
  <CheckBox x:Name="chkBox" />
</Grid>
public class DerivedCtl : ctlBase
{  }

Thanks for any insights...


If I interpret your question correctly, what you want is a UI which is composed of a set of common controls along with another set of controls which are determined by other logic.

Here's one way of doing this.

Create a dialog/form/user control which contains the "base" controls

Add a Grid which will contain the "derived" controls

Create one or more additional user controls to contain the "derived" controls

Programatically add logic to create an instance of the appropriate "derived" user control and add it to the Grid. This of course would happen at runtime.

Maybe the WPF gurus will have a more sophisticated answer...

0

精彩评论

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

关注公众号