简体   繁体   中英

c# wpf datagrid dynamic cell type in one column - ComboBox or Text

I am making a WPF GUI where I have a datagrid with several columns. I would like to make cell type in one of the columns vary depending on the selection in another cell. A cell would need to swap between Text and ComboBox.

In the image below the datagrid can be seen. I want functionality where when user checks the IsConstant checkbox, then he can enter a constant numeric value, otherwise he would be presented with a ComboBox from which he could select an option he likes. The checkbox column is bound to a bool value in codebehind.

数据网格

However I am not sure how and If this is possible. Any suggestions are welcome. Thanks.

I think the only way to achieve what you want is through the use of a DataGridTemplateColumn . You can define a custom template for this column type, which contains controls for each type of edit option you want to provide, with the visibility of each control switching depending on the conditions you have.

I did a search, and found This answer which deals with this very issue, with an example provided. I think you could probably make a simpler template than is given in this example, but the idea is the same.

Edit: Here's an alternate example which uses a template selector (too long to post inline here).

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