none
Customizing ListView cell text color and column fonts RRS feed

  • Question

  • I'd like to do some customizations to a WPF ListView in GridView mode.
    In particular:

    1) I'd like to set a different font size for two particular columns of the grid.

    2) I'd like to change the text color of some cells in a given column, according to some condition (e.g. if, on a given row, the cell in column X has the same content of the cell in column Y, I'd like to change the color of cell in column X to some value).

    All these kinds of things can be done in Win32 using custom draw techniques; but how can this be achieved in WPF?

    Thanks,
    Giovanni

    Wednesday, November 17, 2010 12:45 PM

Answers

  • You can find the details about doing the column headers here: http://vbcity.com/blogs/xtab/archive/2009/09/03/wpf-listview-column-and-cell-formatting.aspx

    For the formatting I'd recomend binding your ListView to an ObservableCollection<ViewModel>. Here each view model is one row of your list view. Expose value and color properties for each row:

    ViewModel.AccountId
    ViewModel.AccountIdColor

    Then in your Xaml as shown in the link above, you set the rows color to be bound to the color property on the VM. Next in your setter for AccountId you execute the logic to examine the VM properties, determine the correct color and then set bound color property. As long as your VM implements INotifyPropertyChanged then you will get dynamic updates.

    Note: MVVM is a rather large topic and if you aren't familiar with it then I'd recomend starting with these articles on how to use this style of programming as it will make your live much easier in the WPF world.

    http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
    http://reedcopsey.com/series/windows-forms-to-mvvm/


    If my response answers your question, please mark it as the "Answer" by clicking that button above my post.

    My blog: http://www.RyanVice.net/
    Wednesday, November 17, 2010 1:12 PM