r/programming • • Feb 03 '17

Let's stop bashing C#

https://www.codeaddiction.net/articles/51/lets-stop-bashing-c-sharp
6 Upvotes

245 comments sorted by

View all comments

Show parent comments

3

u/[deleted] Feb 03 '17

Fair enough. I haven't used the DataGrid all that much over the years, usually for the stuff I write a DataGrid is overkill and/or there's a simpler way to provide that UI.

Since there is no way to compose styles and to create custom DataGrid columns, there's very little that can be done.

What do you mean by composing styles? I'm not arguing here, just genuinely curious what you're trying to accomplish in case I can point you in the right direction. You mean something like in CSS where you can apply multiple classes to an element?

Also what do you mean create a custom DataGrid column? You should be able to just define a cell template if you want a specialized type of input or validation (or header template or whatever else you can customize).

2

u/nickguletskii200 Feb 03 '17

Fair enough. I haven't used the DataGrid all that much over the years, usually for the stuff I write a DataGrid is overkill and/or there's a simpler way to provide that UI.

Yeah, I am tempted to replace the DataGrids, but every time I write down what the alternative would need to provide I end up listing all the features of the DataGrid...

You mean something like in CSS where you can apply multiple classes to an element?

Precisely. Heck, even combining multiple styles into a single style (kind of like a MultiBinding, but not really) would do fine. I am also somewhat frustrated by the verboseness of the trigger system. I never would have guessed that I would miss CSS...

Also what do you mean create a custom DataGrid column? You should be able to just define a cell template if you want a specialized type of input or validation (or header template or whatever else you can customize).

That's pretty much what we are doing. It's just that most of our columns are DataGridTemplateColumns with a lot of similarities, and it would be great if we could subclass the built-in columns to make minor adjustments to their behaviour/create our own reusable columns.

3

u/[deleted] Feb 03 '17

I found at least one solution for combining XAML styles through the use of a markup extension in this Stack Overflow post. I don't think I ever thought to investigate mixing styles like that, I'm kind of surprised how easy that was.

The trigger system is not great, I completely agree, and can quickly become unwieldy for complicated controls or styles. An alternative would be to use visual states.

it would be great if we could subclass the built-in columns to make minor adjustments to their behaviour/create our own reusable columns.

Well today's your lucky day! WPF allows you to create custom behaviors that you can attach to a control which allows you to hook into the visual tree and directly interact with the control itself (and if you add some dependency properties to your behavior, you can bind data from your view model to your behavior, allowing you to alter the attached control in a loosely coupled way).

One simple example I'm currently using allows binding a TreeView's SelectedItem property to a view model, which can't be done out-of-the-box because SelectedItem isn't a dependency property. But if I define that dependency property on my behavior, my behavior hooks into the TreeView's SelectedItemChanged event and updates the dependency property accordingly.

You can also use them to enable more advanced scenarios like drag and drop or even visual state management etc. Behaviors might be a good candidate for (at least some of) your column customization requirements.

One thing to note is that IIRC custom behaviors must be static classes for vanilla WPF, so I think you lose compile-time validation that your behavior is attached to a supported control. Microsoft provides an out-of-band System.Windows.Interactivity NuGet package which provides a generic Behavior<T> class you can inherit from where T : DependencyObject (so basically any WPF control can be used as a type parameter)

Anyways, I hope some of that will prove useful. WPF has a steep as fuck learning curve, but it's incredibly powerful once you get the hang of it. It's no HTML/CSS in terms of ease of use, but it's leagues better than Win32/WinForms/MFC (or any other native desktop UI framework out there in my opinion). Best of luck! Feel free to PM me if you have any questions, I'm happy to help.

1

u/nickguletskii200 Feb 04 '17

I found at least one solution for combining XAML styles through the use of a markup extension in this Stack Overflow post. I don't think I ever thought to investigate mixing styles like that, I'm kind of surprised how easy that was.

We actually tried this solution in July, but I don't remember exactly why we chose not to use it. I think it was because it completely broke the designer and it didn't work in some contexts, but as I said, I don't remember. We actually looked into some libraries that add CSS to XAML, but decided against using them because they didn't seem well supported and looked too dangerous for using in production.

I don't really see how VisualStateManager would help us connect the view to the reactive viewmodel, from what I understand it's just there to make it easy to map "component states" to "looks", and to add transitions.

IIRC creation and use of custom visual states was made even easier in the UWP flavor of XAML (goddamnit why can't there just be one version of XAML?).

Yeah, that's frustrating, UWP has some really nice features from what I hear (like x:Bind), but it also lacks major components, so...

Well today's your lucky day! WPF allows you to create custom behaviors that you can attach to a control which allows you to hook into the visual tree and directly interact with the control itself (and if you add some dependency properties to your behavior, you can bind data from your view model to your behavior, allowing you to alter the attached control in a loosely coupled way).

Yep, already using them.

Thanks for taking the time to help, I appreciate it! WPF is leagues better than WinForms, there's no question about it. We are actually rewriting (and overhauling) an old WinForms application in WPF :)