简体   繁体   中英

WPF (C#) Usercontrol remove elements

I am making a template using Usercontrol in WPF(C#).

However, when applying this user control, is it possible to subtract a specific part? For example, removing a button?

It sounds like you are just trying to hide an existing button, which you should do by setting Visibility to Visibility.Collapsed or Visibility.Hidden . This should be done through a binding to the ViewModel of your user control.

If you need a pure XAML solution: No it is not possible as such. However, the reverse is possible: you can add content to a user control , and that effectively provides the same functionality.

What you could do is make a base user control that doesn't contain the button, and instead has a content presenter. A second user control could wrap the base user control and define a button as its content. Then when you don't want to use the user control with the button you can simply create an instance of the base user control.

To substract specific parts from UserControl, Visibility (Collapse, Hidden) option can be used. Make sure to add dependency property in UserControl for Visibility to show & hide specific part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM