WPF interview questions part-II

Q: Why should I bother wrapping items in a ComboBoxItem?
ComboBoxItem exposes some useful properties—IsSelected and IsHighlighted—and
useful events—Selected and Unselected. Using ComboBoxItem also avoids a quirky behavior with showing content controls in the selection box (when IsEditable is false): If an item in a ComboBox is a content control, the entire control doesn’t get displayed in the selection box. Instead, the inner content is extracted and shown. By using ComboBoxItem as the outermost content control, the inner content is now the entire control that you probably wanted to be displayed in the first place.
Because ComboBoxItem is a content control, it is also handy for adding simple strings to a
ComboBox (rather than using something like TextBlock or Label).
Q: How can I make ListBox arrange its items horizontally instead of vertically?
One way is to define a new control template, but all ItemsControls provide a shortcut with its ItemsPanel property. ItemsPanel enables you to swap out the panel used to arrange items while leaving everything else about the control intact. ListBox uses a panel called VirtualizingStackPanel to arrange its items vertically, but the following code replaces it with a new VirtualizingStackPanel that explicitly sets its Orientation to Horizontal:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation=”Horizontal/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
The translation of this XAML to procedural code is not straightforward, but here’s how you
can accomplish the same task in C#:
FrameworkElementFactory panelFactory =
new FrameworkElementFactory(typeof(VirtualizingStackPanel));
panelFactory.SetValue(VirtualizingStackPanel.OrientationProperty,
Orientation.Horizontal);
myListBox.ItemsPanel = new ItemsPanelTemplate(panelFactory);
Q: How can I get ListBox to scroll smoothly?
By default, ListBox scrolls on an item-by-item basis. Because the scrolling is based on each
item’s height, it can look quite choppy if you have large items. If you want smooth scrolling,
such that each scrolling action shifts the items by a small number of pixels regardless of
their heights, the easiest solution is to set the ScrollViewer.CanContentScroll attached
property to false on the ListBox.
Be aware, however, that by making this change you lose ListBox’s virtualization functionality.
Virtualization refers to the optimization of creating child elements only when they become
visible on the screen. Virtualization is only possible when using data binding to fill the
control’s items, so setting CanContentScroll to false can negatively impact the performance
of data-bound scenarios only.
Q: How can I sort items in a ListBox (or any other ItemsControl)?
Sorting can be done via a mechanism on the ItemsCollection object, so it applies equally to all ItemsControls. ItemsCollection has a SortDescriptions property that can hold any number of System.ComponentModel.SortDescription instances. Each SortDescription describes which property of the items should be used for sorting and whether the sort is ascending or descending. For example, the following code sorts a bunch of ContentControl items based on their Content property:
// Clear any existing sorting first
myItemsControl.Items.SortDescriptions.Clear();
// Sort by the Content property
myItemsControl.Items.SortDescriptions.Add(
new SortDescription(“Content”, ListSortDirection.Ascending));
Q: How do I get the items in my ItemsControl to have Automation IDs, as seen in tools like UISpy?
The easiest way to give any FrameworkElement an Automation ID is to set its Name property, as that gets used by default for automation purposes. However, if you want to give an element an ID that is different from its name, simply set the AutomationProperties.AutomationID attached property (from the System.Windows.Automation namespace) to the desired string.
Q: How can I get items in a StatusBar to grow proportionally?
It’s common to want StatusBar panes that remain proportionately sized. For example,
perhaps you want a left pane that occupies 25% of the StatusBar’s width and a right pane
that occupies 75% of the width. This can be done by overriding StatusBar’s ItemsPanel with
a Grid and configuring the Grid’s columns appropriately.
Q: How can I make TextBox support multiple lines of text?
Setting AcceptsReturn to true allows users to press the Enter key to create a new line of
text. Note that TextBox always supports multiple lines of text programmatically. If its Text is
set to a string containing NewLine characters, it displays the multiple lines regardless of the
value of AcceptsReturn. Also, the multiline support is completely independent from text
wrapping. Text wrapping only applies to individual lines of text that are wider than the
TextBox.
Q: What unit of measurement is used by WPF?
All absolute measurements, such as the numbers used in this section’s size-related properties,
are specified in device-independent pixels. These “logical pixels” are meant to represent
1/96th of an inch, regardless of the screen’s DPI setting. Note that device-independent
pixels are always specified as double values, so they can be fractional.
The exact measurement of 1/96th of an inch isn’t important, although it was chosen
because on a typical 96 DPI display, one device-independent pixel is identical to one physical
pixel. Of course, the notion of a true “inch” depends on the physical display device. If an
application draws a one-inch line on my laptop screen, that line will certainly be longer than
one inch if I hook up my laptop to a projector!
Q: Where’s the entry point in WPF application?
When you create a WPF Windows Application in Visual Studio, the generated project has no Main method, yet it still runs as expected! In fact, even attempting to add a Main method
gives a compilation error telling you that it is already defined.
Application is special-cased when it is compiled from XAML, because Visual Studio assigns
the XAML file a Build Action of ApplicationDefinition. This causes a Main method to be
autogenerated.
Q: How do I retrieve command-line arguments in my WPF application?
Command-line arguments are typically retrieved via a string array parameter passed to Main,
but the common way to define WPF applications doesn’t allow you to implement the Main
method. You can get around this in two different ways. One way is to forego defining an
Application-derived class in XAML, so you can manually define the Main method with a
string array parameter. The easier way, however, is to simply call
System.Environment.GetCommandLineArgs at any point in your application, which returns
the same string array you’d get inside Main.
Q: What is BAML?
BAML, which stands for Binary Application Markup Language, is simply XAML that has been parsed, tokenized, and converted into binary form. Although any chunk of XAML can be represented by procedural code, the XAML-to-BAML compilation process does not generate procedural source code. So, BAML is not like Microsoft intermediate language (MSIL); it is a compressed declarative format that is faster to load and parse (and smaller in size) than plain XAML. BAML is just an implementation detail of the XAML compilation process without any direct public exposure, so it could be replaced with something different in the future. Nevertheless, it’s interesting to be aware of its existence.
Q: What is XAML?
XAML is used extensively in .NET Framework 3.0 technologies, particularly in Windows Presentation Foundation (WPF) . In WPF, XAML is used as a user interface markup language to define UI elements, data binding, eventing, and other features. In WF, workflows can be defined using XAML.
XAML elements map directly to common language runtime object instances, while XAML attributes map to Common Language Runtime properties and events on those objects. XAML files can be created and edited with visual design tools such as Microsoft Expression Blend, Microsoft Visual Studio, and the hostable Windows Workflow Foundation visual designer.
Q: What is XBAP?
XBAP stands for XAML Browser Application. XBAP allows for WPF applications to be used inside a browser. The .NET framework is required to be installed on the client system. Hosted applications run in a partial trust sandbox environment. They are not given full access to the computer’s resources and not all of WPF functionality is available.
WPF supports the creation of applications that run directly in a web browser. (So will WPF/E, when it is released.) They are called XAML Browser Applications (XBAPs), and have a .xbap file extension.
The power of this WPF support is that the exact same programming model is used for a XAML Browser Application as for a standard Windows application. Therefore, creating an XBAP isn’t much different than creating a standard Windows application. The main differences are as follows:
. Not all features in WPF or the .NET Framework are accessible (by default).
. Navigation is integrated into the browser (for Internet Explorer 7 or later).
. Deployment is handled differently.
Q: What is a Routed event?
In a typical WPF application, it contains many elements. These elements exist in an element tree relationship with each other. A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.

No comments:

Post a Comment