Learn About Microsoft .NET Framework 3.5
By Certification Magazine —
1 | 2 | 3 |
You should not use the following code segment:
BitmapImage bitmap = (BitmapImage)Application.Current.Resources["ResourceImage"];
Transform transform = new Transform(TransformKind.Rotate,180);
TransformedBitmap transformedBitmap = new TransformedBitmap(bitmap, transform);
Image image = new Image();
image.Source = transformedBitmap;
Although this code correctly retrieves the bitmap and assigns it to the Source property of the Image instance, the transform applied to the bitmap is invalid. The Transform class is abstract, and there is no TransformKind enumeration. Instead you should use the RotateTransform class to perform the transformation.
You should not use the following code segment:
BitmapImage bitmap = new BitmapImage(new Uri("pack://application:,,,/ResourceImage"));
RotateTransform transform = new RotateTransform(180);
TransformedBitmap transformedBitmap = new TransformedBitmap(bitmap, transform);
Image image = new Image();
image.Source = bitmap;
This code attempts to create a BitmapImage instance by retrieving the image as a binary resource. However, in this scenario, the bitmap is defined as a logical resource in the application definition file. Also, although this code creates a TransformedBitmap instance, it sets the Source property of the Image control to the BitmapImage instance that represents the original bitmap. The original bitmap is not rotated.
References:
Imaging Overview, MSDN, http://msdn2.microsoft.com/en-us/library/ms748873.aspx
How to: Chain BitmapSource Objects Together, MSDN, http://msdn2.microsoft.com/en-us/library/ms743719.aspx
How to: Apply a Transform to a BitmapImage, MSDN, http://msdn2.microsoft.com/en-us/library/aa970271.aspx
Objective: Bind to data sources.
Sub-objective: Bind to a data collection.
Single answer, multiple-choice
You are developing a Microsoft WPF application. A ListBox control named employeeListBox exists in the XAML file for a window. You write the following code to create a collection of employees:
EmployeeCollection employees = new EmployeeCollection();
employees.Add(new Employee(){Name = "Employee 1"});
employees.Add(new Employee(){Name = "Employee 2"});
employees.Add(new Employee(){Name = "Employee 3"});
employeeListBox.DataContext = employees;
You need to configure the ListBox control to display the value of the Name property of each Employee instance in the collection. Which XAML content should you use?
A.
Name="employeeListBox"
ItemsSource="{Binding ElementName=Name}"/>
B.
Name="employeeListBox"
ItemsSource="{Binding Source=Employee.Name}"/>
C.
Name="employeeListBox"
ItemsSource="{Binding Path=Name}"/>
D.
Name="employeeListBox"
ItemsSource="{Binding Source=Employee}"/>
Answer:
C
Tutorial:
You should use the following XAML content:
Name="employeeListBox"
ItemsSource="{Binding Path=Name}"/>
This XAML sets the ItemsSource property to a value that uses the Binding markup extension syntax. The Path property of the Binding class specifies the source value of a binding. Typically, you would also set either the Source property or the ElementName property to specify the binding source. However, in this scenario, the DataContext property of the ListBox control is set. When the DataContext property is set, a binding uses its value as the binding source. This allows child controls to reuse the binding source associated with a parent control.
You should not use the following XAML content:
Name="employeeListBox"
ItemsSource="{Binding ElementName=Name}"/>
This XAML sets the ElementName property to Name, and it does not set the Path property. The ElementName property specifies a XAML element to which a binding applies. In this scenario, the binding does not apply to another XAML element.
You should not use the following XAML content:
Name="employeeListBox"
ItemsSource="{Binding Source=Employee.Name}"/>
This XAML sets the Source property to Employee.Name, and it does not set the Path property. The Source property specifies the instance of an object being bound. In this scenario, because the DataContext property of the ListBox control is set, you should not set the Source property of the Binding class.
You should not use the following XAML content:
Name="employeeListBox"
ItemsSource="{Binding Source=Employee}"/>
This XAML sets the Source property to Employee, and it does not set the Path property. The Source property specifies the instance of an object being bound. In this scenario, because the DataContext property of the ListBox control is set, you should not set the Source property of the Binding class.
References:
Binding Declarations Overview, MSDN, http://msdn2.microsoft.com/en-us/library/ms752300.aspx
How to: Specify the Binding Source, MSDN,
http://msdn2.microsoft.com/en-us/library/ms746695.aspx
How to: Bind to a Collection and Display Information Based on Selection, MSDN, http://msdn2.microsoft.com/en-us/library/aa970558.aspxData Binding Overview, MSDN, http://msdn2.microsoft.com/en-us/library/ms752347.aspx 1 | 2 | 3 |




