开发者

What is the best way to replace controls at runtime?

I'm fairly new to WPF and I have this scenario:

I have an application that contains an area where different sets of controls should be displayed at different time(different application states).

I'm wondering what is the approach in WPF?

In winforms I would 开发者_如何学运维make controls visible/invisible at runtime. If there were too many controls I would group them on Panels/UserControls and show/hide those.

My gut tells me there is a better way in WPF.


There are lots of options for doing this in WPF. In addition to hiding and showing individual or groups of controls by setting Visibility, you could use different DataTemplates to contain the set of controls for each state and switch between those. All you need is a ContentControl on which you can set the ContentTemplate. The ContentTemplate value can then be switched to different DataTemplates using a Trigger, a Binding, from code, or by using ContentTemplateSelector to choose a template.


The Visibility property describes your old winforms habits perfectly.

You'll also want to look into Visual States. This will allow you to hide/show multiple controls and even change other properties (i.e. enable state, font color).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜