12/22/2023 0 Comments Wpf colorconverter![]() Setting time and date to date picker and time picker in android Keywords : c#,asp.net,query-string Read More. Īccess images inside public folder in laravel Keywords : c#,facebook,facebook-graph-api,asp.net-mvc-5,asp.net-identity-2 Read More. Why new fb api 2.4 returns null email on MVC 5 with Identity and oauth 2? How to subtract one array from another, element-wise, in javascript Keywords : asp.net,asp.net-mvc,model-binding Read More. ![]() Keywords : scala,scala-2.8,scala-collections Read More. How to convert a Seq to a Map using a value of A as the key in the map? Keywords : language-agnostic,code-golf,rosetta-stone Read More. How to set or get a cookie value in django You can expand this code to translate name to Media.Color directly. This code makes translating name to Color class faster: public class FastNameToColor It's a bit ugly, but it's a one-time hit.įyi, another easier way is just use microsoft built static class, ex Colors.Red Public static Color FromName( string name)įoreach ( PropertyInfo property in typeof( Colors). ![]() You could fairly easily fetch the property names and values from once into a map: private static readonly Dictionary KnownColors = FetchColors() If you want to avoid boxing, build a dictionary up to start with for the standard names (still using ColorConverter) and then use the dictionary for subsequent lookups. Call ColorConverter.ConvertFromString and cast the result. Of course, ColorConverter is the way to go. var result = ColorConverter.ConvertFromString( "Red") as Color Creating the from the ARGB values of the .Īnswer 1: var color = (Color)ColorConverter.ConvertFromString( "Red").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. 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |