What is the easiest way to break up large XAML files in my application?

后端 未结 3 2068
灰色年华
灰色年华 2021-02-08 12:57

I recently discovered that I could make use of user controls to reduce the size of my main application\'s .xaml file. I am new to WPF and realized that my app\'s XAML is getting

3条回答
  •  生来不讨喜
    2021-02-08 13:32

    User controls are a very good way to isolate code out of the main window. In addition to decluttering, they also provide other advantages such as modularizing the code and providing a limited interface into that section of code, which helps improve maintainability and helps prevent spaghetti code.

    In addition to that, DataTemplates can also be useful. For example, suppose you have a bunch of fields that need to be entered in and all of those fields have labels. In that case, you can create a class with two properties, one for the label and one for the value of that field. You can then create a DataTemplate for that class that binds the label to a TextBlock and the value to a TextBox. If you want all of the labels to line up, you can create a Grid SharedSizeScope. After you do that, you can then create an ObservableCollection of that class, fill the collection with labels and values, and then set the ItemsSource of an ItemsControl to it. After you get the initial plumbing of this going, data entry forms can be generated way faster than they can in WinForms.

提交回复
热议问题