ListView Template dynamically modify data in a column.

There is a demand: a column for ListView, content display different data requirements are not the same. For example, when the column bound a Bool type attribute, when the value true, display a picture, when the value is false, displaying a button or other control.

In response to this demand, the solution generally think of is to create two different data templates:

Then create a DataTemplateSelector, the template selector to output a different value depending on the templates.

Then in the ListView, you can assign for GridViewColumn of CellTemplateSelector.

If the data is just a static display, use this approach can solve the problem. But the problem is that if the program is running, the value of the cell changes found CellTemplateSelector not work anymore, because he was not bound value changes automatically switches template. Therefore, this template selector way for my needs are not met, need to find another way. Through the network to find, to find a solution on the MSDN forums, the core idea is to trigger Datatrigger through data to dynamically change the template.

The first step: define two different data templates;

Step two: define a data type for the current template, and then placed in a ContentControl data template, we can increase the data triggers Style change in Contentcontrol to trigger modification of the ContentControl ContentTemplate properties based on different values ​​and achieve demand. As follows (picture be taken from the original source):

 

Guess you like

Origin www.cnblogs.com/chyshx/p/11330690.html