Tag Archives: WPF

Saving & Restoring WPF DataGrid Columns’ Size, Sorting and Order

A data grids usually allow users to reorder, resize, and sort its columns. A refined data grid implementation remembers the user’s changes to its columns’ display, restoring the columns to match how they previously appeared each time the application is launched. How do we implement such a feat if WPF’s DataGrid is our control of choice? Continue reading

WPF: Separate Grids, Auto-Sized Columns/Rows With Synchronized Dimensions

The design specification calls for two Grids. One column in each is to auto-size its width to its content. “No problem—<ColumnDefinition Width=”Auto” /> will take care of that,” you think. Then you notice a footnote: “The widths both auto-sized columns should match. Both columns should be the width of the wider column’s content.

How do you implement this? Continue reading

Master-Detail Forms & Object Scope (Context)

Imagine a master-detail form. The top portion of the window contains a grid listing people’s names. Below is a form where the currently-selected person’s details may be edited. Should data-binding be used to connect the edit form’s fields directly to the grid’s currently selected record (row)? To put it another way, should the grid’s selected item and the edit form both be referencing the same in-memory object instance? Continue reading

How is a WPF XAML file tied into the application’s executable environment?

By hidden source code file…

A hidden source code file is auto-generated for each WPF XAML file. The names used for this file and for the class it contains are based off the XAML file’s filename. In a C# application, the file MyWindow.xaml will have a hidden code-behind file named MyWindow.g.cs containing a class named MyWindow.

…containing a class…

The contained class inherits from the WPF core class corresponding to the associated XAML file’s root element. If the root element in the XAML file is <Window>, the class in the hidden code-behind file will inherit from Window.

Continue reading

Keeping the Selected Item Selected When Changing a ListView’s ItemsSource

A ListView’s data display needs to be updated. The revised dataset, retrieved from the service layer, is contained in a new collection containing new object instances. Setting this IList<T> as the control’s new ItemsSource causes the control to display the revised data and…whoa…clears the currently selected item.

From the technical perspective, this clearing makes sense. A new collection has been displayed making the selected item from the previous collection irrelevant. However, from the user’s perspective, the same collection is displayed both before and after the refresh (albeit with revised data), so the current item selection is still pertinent and should be maintained.

Continue reading

WPF, Where Is Your “static Main()” Method?

The typical WPF application—as viewed in Solution Explorer—consists primarily of XAML files (.xaml) paired with code-behind files (.xaml.cs) and ordinary source code files (.cs). None of these files contains a static Main() method—the starting point of execution in a C# program. Where then does execution begin in a C#.Net WPF application?

Continue reading

Event Arguments == Static Snapshots?

Is the event argument that’s passed to an event handler/handler override always a non-changing snapshot taken the moment the event occurred?

No! While convention suggests a “yes” answer, no technical constrain enforces this assumption (as I discovered recently). The properties of an event argument can (and sometimes do) return information about the current (live) state of the system instead of describing the moment the event occurred.

Continue reading