What is DataContext ? How is it used ?
Every FrameworkElement can be associated with a DataContext which will be used as the default data source during binding, if no other data source is specified in the binding code. Also, the children of this FrameworkElement auotmatically inherit this setting. This helps having to repeatedly specify this data source as the source of a binding, thereby letting you simplify the binding code. The following code snippet is an example of implicit datasource using DataContext. [XAML] <StackPanel x:Name=’Class’ DataContext='{StaticResource student}’> <Label x:Name=’studcount’ Content='{Binding Path=Count}’/> <ListBox x:Name=’Studname’ DisplayMemberPath=’Name’ ItemsSource='{Binding}’/> </StackPanel>
How to access elements that are embedded inside the ControlTemplate ?
When you declare elements inside the ControlTemplate, use the ‘Name’ of each control for identifying it from the outside. We can then use the ‘FindName’ function for finding the resource. An example shown below is for accessing the TextBlock of a button. [XAML] <Button Background=’LightGoldenrodYellow’> <Button.Style> <Style TargetType=’Button’> <!–Set to true to not get any properties from the themes.–> <Setter Property=’OverridesDefaultStyle’ Value=’True’/> <Setter Property=’Template’> <Setter.Value> <ControlTemplate TargetType=’Button’> <Grid> <Ellipse Name=’innerEllipse’ Fill='{TemplateBinding Background}’/> <ContentPresenter HorizontalAlignment=’Center’ VerticalAlignment=’Center’/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Style> </Button> [C#] // From inside the custom Button type: Ellipse ellipse = this.Template.FindName(‘innerEllipse’, this) as Ellipse;
What are the requirements for a property to be animated?
WPF enables users to apply animations to objects by applying animations on properties. For a property to have animation capabilities, it should have the following requirements. The property should be a dependency property. It must belong to a class that inherits from the DependencyObject class and implements the IAnimatable interface. There must be a compatible Animation type available for the property. For e.g. the Double type property can be animated using the DoubleAnimation. Similarly, the property to be animated should contain the corresponding animation type.
How lights are useful in 3D graphics ?
Lights are similar to lights in real life. Light objects are used to illuminate a part of a scene. Lights can be used to create shadow effects. In a 3D scene at least one light has to be added to make the scene visible. WPF has four different types of lights with unique functionalities. The following are the types of light available in WPF. AmbientLight – provides uniform lighting to all the objects in a 3D scene. DirectionalLight – It is like a distant light source. It does not have a location but has a direction. PointLight – It illuminates like a nearby light source. It has a location and provides light effect from the location and objects in the scene are illuminated based on the location and position of the point light. SpotLight – It is similar to a pointlight. It provides illumination in a cone shape. The ‘InnerConeAngle’ and ‘OuterConeAngle’ properties determine the illumination of the objects in the 3D scene.
How can I get an enumerator to the ContentControl’s logical child elements ?
The LogicalChildren of a ContentControl is of ‘protected-type’ access. You can derive a custom class from it and then expose a public version of the ‘LogicalChildren’ property. [C#] public class SPanel : StackPanel { public System.Collections.IEnumerator LogicalChildren { get { return base.LogicalChildren; } } }