Silverlight Custom ItemsControl
ItemsControl Class
Represents a control that can be used to present a collection of elements.
- Namespace: System.Windows.Controls
- Assembly: System.Windows (in System.Windows.dll)
Syntax - in VB
Code:
'Declaration <ContentPropertyAttribute("Items", True)> _
_ Public Class ItemsControl
_ Inherits Control
Implements IGeneratorHost
'UtilizationDim instance As ItemsControl
XAML
Code:
<... ItemsControl>
oneOrMoreItems
</ ItemsControl>
In general, an ItemsControl has two roles. It can be used to present a fixed set of elements or to view a list obtained from data binding to an object. There are several controls that inherits from ItemsControl that you can use in your application, but you can also directly use an ItemsControl. For example, if you have a collection of data that you want to display as a list of items not selectable, you can use an ItemsControl to contain the data. Whether you use the ItemsControl or a child class, you can use the ItemTemplate to specify the appearance of each element. If you want to display a fixed list, complete Items with one or more child objects FrameworkElement , and let DisplayMemberPath as an empty string by default. Do not specify ItemsSource.
If you want to display data from a source object, specify ItemsSource as a reference to an object. You usually perform this operation through a declaration Binding , and the resolution mechanism of the object used as a source of binding is possibly influenced by the DataContext into force. You can set DisplayMemberPath as the name or path of private property that should appear as given repeatedly by default for the object returned. You can leave DisplayMemberPath empty string as default and specify a DataTemplate to the ItemTemplate . This approach allows you more control over the visual representation of each element and also show several properties of each object related to the data in each cell, using a layout you define in the model.
Code:
<UserControl x: Class = "TrialListBoxSnippet.Page"
xmlns = "http://schemas.microsoft.com/winfx/2010/xaml/presentation"
xmlns: x = "http://schemas.microsoft.com/winfx/2010/xaml"
xmlns: src = "clr-namespace: TrialListBoxSnippet"
>
<StackPanel x: Name = "LayoutRoot" Background = "White" Margin = "10,10,10,10">
<StackPanel Orientation = "Horizontal">
<TextBlock Margin = "5" Text = "ListBox with unbound data:" />
<ListBox Width = "150" Margin = "0,5,0,10">
<TextBlock Text = "TextBlock" />
<TextBox Text = "TextBox" />
<Button Content = "Button" />
<Rectangle Fill = "LightBlue" Height = "20" Width = "100" Margin = "2,2,2,2" />
<Ellipse Fill = "Coral" Height = "20" Width = "150" Margin = "2,2,2,2" />
</ ListBox>
<TextBlock Margin = "5" Text = "ListBox bound with data:" />
<Grid>
<Grid.Resources>
<Src: Customers x: Key = "customers" />
</ Grid.Resources>
<ListBox ItemsSource = "(StaticResource Customers)" Width = "250" Margin = "0,5,0,10"
DisplayMemberPath = "LastName" />
</ Grid>
</ StackPanel>
<StackPanel Orientation = "Horizontal">
<TextBlock Margin = "5" Text = "ListBox ItemTemplate with:" Width = "160" />
<Grid>
<Grid.Resources>
<Src: Customers x: Key = "customers" />
</ Grid.Resources>
<ListBox ItemsSource = "(StaticResource Customers)" Width = "350" Margin = "0,5,0,10">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation = "Horizontal">
<TextBlock Padding = "5,0,5,0"
Text = "(Binding FirstName)" />
<TextBlock Text = "(Binding LastName)" />
<TextBlock Text = "" />
<TextBlock Text = "(Binding) Address" />
</ StackPanel>
</ DataTemplate>
</ ListBox.ItemTemplate>
</ ListBox>
</ Grid>
<Grid>
<Grid.Resources>
<Style x: Key = "horizontalListBoxStyle" TargetType = "ListBox">
<Setter Property = "ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation = "Horizontal"
VerticalAlignment = "Center"
HorizontalAlignment = "Center" />
</ ItemsPanelTemplate>
</ Setter.Value>
</ Setter>
<Setter Property = "ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation = "Horizontal">
<TextBlock Padding = "5,0,5,0"
Text = "(Binding FirstName)" />
<TextBlock Text = "(Binding LastName)" />
</ StackPanel>
</ DataTemplate>
</ Setter.Value>
</ Setter>
</ Style>
<Src: Customers x: Key = "items" />
</ Grid.Resources>
<ListBox Height = "25" Width = "420" ItemsSource = "(items) StaticResource" Style = "(StaticResource horizontalListBoxStyle)" />
</ Grid>
</ StackPanel>
<TextBlock Margin = "5" Text = "ListBox with selectionChanged event handler:" Width = "270 'HorizontalAlignment =" Left "/>
<ListBox Width = "150" Margin = "0,7,0,7" selectionChanged = "PrintText" HorizontalAlignment = "Left">
<ListBoxItem Content = "Item 1" />
<ListBoxItem Content = "Item 2" />
<ListBoxItem Content = "Item 3" />
<ListBoxItem Content = "Item 4" />
<ListBoxItem Content = "Item 5" />
</ ListBox>
<TextBlock Name = "textBlock1" />
</ StackPanel>
</ UserControl>
Re: Silverlight Custom ItemsControl
In general, an ItemsControl has two functions. It can be used to present a fixed set of elements or to display a list obtained from data binding to an object. There are several controls that inherit from ItemsControl that you can use in your application, but also can directly use an ItemsControl. For example, if you have a collection of data to display a list not selectable, you can use ItemsControl to contain the data. Regardless of whether you use ItemsControl or a child class, you can use ItemTemplate to specify the appearance of each item. To display a fixed list, fill Items with one or more child objects FrameworkElement and let DisplayMemberPath as default empty string. Do not specify ItemsSource.
If you want to display data from a source object, specify ItemsSource reference to an object. This is usually done through a declaration of Binding , and the mechanism of resolution of the object that is used as the source of the link may be affected by the property DataContext active. You can set DisplayMemberPath the name or the path of specific property to be displayed as data replication using a default presentation of the object returned. Or you can leave DisplayMemberPath as default and specify empty string DataTemplate to the ItemTemplate . The latter approach provides more precise control of the visual representation of each item and also displays various properties of each data-bound object within each element, with a design that is defined in the template.
The following example creates a page with several controls ListBox , inherited from ItemsControl, configured in various ways.
Code:
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Ink
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.Collections.ObjectModel
Partial Public Class Page
Inherits UserControl
Public Sub New ()
'Required to initialize variables
InitializeComponent ()
End Sub
PrintText Private Sub (ByVal sender As Object, ByVal args As SelectionChangedEventArgs)
LBI Dim As ListBoxItem = TryCast (TryCast (sender, ListBox). SelectedItem, ListBoxItem)
textBlock1.Text = "You selected" + lbi.Content.ToString () + "."
End Sub
End Class
Public Class Customer
As String Private _firstName
As String Private _lastName
As String Private _address
Public Property FirstName () As String
Get
Return _firstName
End Get
Set (ByVal value As String)
_firstName = value
End Set
End Property
Public Property LastName () As String
Get
Return _lastName
End Get
Set (ByVal value As String)
_lastName = value
End Set
End Property
Public Property Address () As String
Get
Return _address
End Get
Set (ByVal value As String)
_address = value
End Set
End Property
Public Sub New (ByVal firstName As String, ByVal lastName As String, ByVal address As String)
Me. FirstName = firstName
Me. LastName = lastName
Me. Address = address
End Sub
End Class
Public Class Customers
Inherits ObservableCollection (Of Customer)
Public Sub New ()
Add (New Customer ("Megatron", "Death Valley", "12 North Third Street, Apartment 45"))
Add (New Customer ("AI", "High Hell", "34 West Fifth Street, Apartment 67"))
Add (New Customer ("Hitman", "Graveyard", "56 East Seventh Street, Apartment 89"))
Add (New Customer ("SB", "Tunnel", "78 South Ninth Street, Apartment 10"))
End Sub
End Class
Re: Silverlight Custom ItemsControl
Microsoft UI Automation provides a single interface, generalized automation clients can use to examine or operate the user interfaces for various platforms and infrastructures. UI Automation enables both applications to access, such as screen readers and code quality assurance (testing), to review elements of the user interface and to simulate user interaction with them from other code. For more information about UI Automation on all platforms and in its general meaning.
Before implementing a peer Automation for a custom control, you must examine or test if the class of basic control and automation counterpart already provide full accessibility or support Automation you need. In many cases, the combination of implementations FrameworkElementAutomationPeer, homologues and specific models they implement can provide a satisfactory experience of accessibility. It depends on the degree to which you add the object model display of your control. Also, it depends on whether your additions to the functionality of base class are correlated with the new UI elements in the contract model or with the general visual appearance of control and therefore presents new aspects of user experience that require support accessibility.
Classes counterparts Automation
The Silverlight controls support UI Automation through a tree of classes that derive from peer AutomationPeer . By convention, class names begin with the name counterparts of the control class and end with AutomationPeer. For example, ButtonAutomationPeer is the class in the control class Button . Classes counterparts are roughly equivalent to UI Automation control types, but they are specific to Silverlight elements. Automation code that accesses the applications via the Silverlight UI interface does not directly Automation Automation counterparts. However, the automation code that is in the same process space can be used directly. Counterparts Automation must run in a partial trust environment.
Integrated Automation Classes counterparts
The elements implement a class in automation if they accept the activity of the user interface or if they contain the necessary information to users of screen reader applications. Visuals Silverlight does not have any counterparts Automation. For example, the classes Button and TextBox implement counterparts Automation. By cons, classes Border counterparts do not implement an automation, as well as classes based on Panel such as Grid and Canvas.
Class Control base class has no corresponding counterpart. If you need to match a class in a custom control that derives from Control, you must extend the class in custom FrameworkElementAutomationPeer.
Navigation counterpart
After locating a homologue Automation code in-process can navigate the tree counterpart by calling the methods GetChildren and GetParent object. In testing, navigating among the elements Silverlight is supported by the implementation of the peer method GetChildrenCore. The UI Automation system calls this method to generate a tree of sub-elements contained in a control, for example, list items in a list box. The default method GetChildrenCore of FrameworkElementAutomationPeer traverses the tree of visual elements to generate the tree counterparts Automation. Custom controls override this method to expose children to elements of automation clients, turning counterparts Automation elements that convey information or allow the user intervention.
Customizations in a peer derived
All classes that derive from UIElement contain the method protected virtual OnCreateAutomationPeer . Silverlight called OnCreateAutomationPeer to receive the item Automation counterpart for each control. Automation code can use the counterpart for information on features and functionality of a control and to simulate interactive use. A custom control that supports Automation must substitute OnCreateAutomationPeer and return an instance of a class that derives from AutomationPeer . For example, if a custom control is derived from the class ButtonBase , then the object returned by OnCreateAutomationPeer must derive from ButtonBaseAutomationPeer.
The Automation counterpart must define a type constructor that uses a secure control instance owner for basic initialization. Here is a typical example, where the only implementation is to transmit the owner at the base SelectorAutomationPeer. When you implement a custom control, you should override one method, "core" of the class in automation that outline the specific and unique behavior of your custom control.
Replace getPattern
Counterparts Automation simplify certain aspects of implementation of UI Automation providers, but the counterparts Automation Custom Control must still manage interfaces model. Like non-suppliers Silverlight counterparts support the control models by providing implementations of interfaces in the namespace System.Windows.Automation.Provider as IInvokeProvider . The interfaces of the control model can be implemented by the peer itself or by another object. The implementation of the counterpart getPattern returns the object that supports the specified pattern. The code IU Automationcalls method getPattern and specifies an enumeration value PatternInterface. Substituting getPattern should return the object that implements the specified pattern. If your control does not have a custom implementation of a model, you can call the implementation of the basic type of getPattern to recover its implementation, or a null reference if the model is not supported for this type of control. For example, an up-down custom may have a value within a range, so its UI Automation peer implements the interface IRangeValueProvider. The following example shows how the method getPattern of homologous substituted in response to a value PatternInterface.RangeValue.
Code:
Public Overrides Function getPattern (ByVal patternInterface As PatternInterface) As Object
If (patternInterface PatternInterface.RangeValue =) Then
Return Me
End If
Return MyBase. GetPattern (patternInterface)
End Function
A method getPattern can also specify a sub-element as a supplier of models. The following code explains how ItemsControl transfer the management model of scrolling its control counterpart ScrollViewer house. To specify a sub-component for the management model, this code retrieves the object sub-element, creates a peer by using the method CreatePeerForElement and returns the new peer.