I decided I could pass in a flag to indicate whether I wanted the foreground color or the background color. This allows you to pass an additional parameter to an IValueConverter. Then I learned about the ConvertParameter. I never did any coding for this and therefore there is no “Attempt3” tag. This was too complicated and it would have limited my converter to only working against a certain framework element (a DataGridCell in this case). Third Attempt (No Tag)īefore I had talked with Anne Marsan about the MultiValueConverters, I thought about creating a multivalue converter that would take both the framework element I want to affect as well as my enum value and do the property setting right inside the converter. It’s up to the view to decide how this data should be displayed. The view model (in my opinion) should just represent the data to be displayed. My first attempt was brute force and worked, but was messy – I simply created two value converters: both would take my enum, but one would return the background color and one would return the foreground color.īut I didn’t like this idea – it sounds too much like I’m mixing my UI and my view model. Each “attempt” that I detail below is tagged in the repository so you can easily see the implementations of each approach by switching your working directory to a certain tag. NOTE: I’ve provided sample code in a BitBucket project here. I did some research on MultiValueConverters, but, as seasoned WPF developer Anne Marsan pointed out, they take multiple inputs but still only produce a single output. Convert From String (String) Method Reference Feedback In this article Definition Applies to Definition Namespace: System. I’m familiar with value converters, but they only convert to a single value. Yesterday, I came across a situation where I needed a single item from my view model (an enum value) to affect two properties on a WPF element (the Foreground and Background colors of a DataGridCell). That, along with a very testable MVVM-based architecture, has made the experience a fun one! So far, I’m really liking the data binding flexibility WPF gives you. I’ve done a ton of reading on WPF and it’s helped me in my understandings of how WPF works.
0 Comments
Leave a Reply. |