| WPF-Question-Answers-1-10 | WPF-Question-Answers-21-30 | |
WPF (Windows Presentation Foundation) Question Answers -(11–20) |
StaticResource provides a value for a XAML property by substituting the value of an already defined resource. Compiler will throw error during compilation if not found in resources.
DynamicResource provides a value for a XAML property by deferring that value to be a run-time reference to a resource. It will create a temporary expression during the initial compilation and thus defer lookup for resources until the requested resource value is actually required in order to construct an object.
The architecture of WPF is a multilayered architecture. The major code portions of WPF are PresentationFramework, PresentationCore, and milcore.
Of these milcore is an unmanaged component. Milcore is written in unmanaged code in order to enable tight integration with DirectX. All display in WPF is done through the DirectX engine, allowing for efficient hardware and software rendering.
PresentationFramework.dll: It holds the top level WPF elements, including those that represents Windows, panels, controls, styles etc. It also implements the end-user presentation features including time-dependent, story-based animations and data binding.
PresentationCore.dll: Presentation Core provides a managed wrapper for MIL and implements the core services for WPF such as UI Element and visual from which all shapes and controls derived in PresentationFramework.dll.
milCore.dll: The composition engine which renders the WPF application is a native component. It is called Media Integration Library (Layer) (MIL) and resides in milCore.dll. The purpose of the milCore is to interface directly with DirectX and provide basic support for 2D and 3D surface. It is the main rendering engine of WPF and is interface between DirectX and CLR.
Data binding is the process that establishes a connection between the application UI and business logic. If the binding has the correct settings and the data provides the proper notifications, then, when the data changes its value, the elements that are bound to the data reflect changes automatically. Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.
StaticResource is read once during the lifetime of our app. StaticResource will be resolved and assigned to the property during the loading of the XAML which occurs before the application is actually run. It will only be assigned once and any changes to resource dictionary are ignored.
A DynamicResource assigns an Expression object to the property during loading but does not actually look up the resource until runtime when the Expression object is asked for the value. This defers looking up the resource until it is needed at runtime. It will update the target if the source resource dictionary is changed. The value of a DynamicResource is read every time it is used, so if we assign a new instance to it during the lifetime of our app, all references of that resource will get an update.
In WPF the heart of resource management is the ResourceDictionary class. This is just a simple collection class. It behaves much like an ordinary Hashtable: it allows objects to be associated with keys, and it provides an indexer that lets you retrieve those objects using these keys.
Resource Dictionary can be created in two ways:
In C#:
ResourceDictionary myDictionary = new ResourceDictionary();
myDictionary.Add("myBrush", Brushes.Green);
myDictionary.Add("HW", "Hello, world");
In XAML:
<Window.Resources>
<SolidColorBrush x:Key="myBrush" Color="Green" />
<s:String x:Key="HW">Hello, world</s:String>
</Window.Resources>
The x:Key attribute specifies the key that identifies the resource in the dictionary. It is equivalent to the first parameter of the calls to Add in the C# example. You can use any object as a key. Strings are the most common choice, although distinct object instances are often used to identify very broadly scoped resources.
Everything visible on screen is visible because it was painted by a brush in WPF. A brush is used to describe the background of a button, the foreground of text, and the fill of a shape, etc. Brushes enable you to paint user interface (UI) objects with anything from simple, solid colors to complex sets of patterns and images.
Paint with a Solid Color: Paints an area with a solid color.
Paint with a Linear Gradient: Paints an area with a linear gradient. A linear gradient blends two or more colors across a line, the gradient axis. GradientStop is used to specify the colors in the gradient and their positions.
Paint with a Radial Gradient: Paints an area with a radial gradient. A radial gradient blends two or more colors across a circle. Use GradientStop objects to specify the colors in the gradient and their positions.
Paint with an Image: An ImageBrush paints an area with an ImageSource.
Paint with a Drawing: Paints an area with a Drawing. A drawing can contain shapes, images, text, and media.
Paint with a Visual: A VisualBrush paints an area with a Visual object. Examples of visual objects include Button, Page, and MediaElement. A VisualBrush also enables you to project content from one portion of your application into another area; it's useful for creating reflection effects and magnifying portions of the screen.
Every WPF control—whether it is Window, Button, or TextBox—inherits from DispatcherObject.
Object ← DispatcherObject ← DependencyObject ← Visual ← UIElement ← FrameworkElement ← Control ← Button
OR
Object → DispatcherObject → Application → DependencyObject → UIElement → FrameworkElement → Control → ContentControl → Window.
When WPF creates an instance of Button, it calls the protected constructor of DispatcherObject. DispatcherObject contains a property of type Dispatcher. In the constructor, it saves the reference of the current thread’s Dispatcher to the Dispatcher property of DispatcherObject.
Every WPF control—whether it is Window, Button, or TextBox—inherits from DispatcherObject.
Object ← DispatcherObject ← DependencyObject ← Visual ← UIElement ← FrameworkElement ← Control ← Button
OR
Object → DispatcherObject → Application → DependencyObject → UIElement → FrameworkElement → Control → ContentControl → Window.
When WPF creates an instance of Button, it calls the protected constructor of DispatcherObject. DispatcherObject contains a property of type Dispatcher. In the constructor, it saves the reference of the current thread’s Dispatcher to the Dispatcher property of DispatcherObject.
In WPF, controls are typically not directly responsible for their own appearance (“shapeless” controls). Controls in WPF are all about behavior, and they defer to templates to provide their visuals (look and feel). Many GUI frameworks require you to write a custom control when customizing a control’s appearance, but in WPF, this is not necessary—nested content and templates offer simpler yet powerful solutions. You do not need to write a custom control unless you need interactive behavior that is different from any of the built-in controls.
One way is to call the SetBinding method on the target object:
Person myDataSource = new Person("Joe");
Binding myBinding = new Binding("Name");
myBinding.Source = myDataSource;
// myText is an instance of TextBlock
myText.SetBinding(TextBlock.TextProperty, myBinding);
Only FrameworkElements and FrameworkContentElements have a SetBinding method. Their SetBinding method is actually calling the BindingOperations.SetBinding method. Therefore, you can always use the BindingOperations.SetBinding method, especially when your target object is not a FrameworkElement or a FrameworkContentElement.
| WPF-Question-Answers-1-10 | WPF-Question-Answers-21-30 | |