Skip to main content
.NET Framework Class Library
ViewBase Class

Represents the base class for views that define the appearance of data in a ListView control.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
Public MustInherit Class ViewBase _
	Inherits [%$TOPIC/ms617879_en-us_VS_110_3_0_0_0_0%]
public abstract class ViewBase : [%$TOPIC/ms617879_en-us_VS_110_3_0_1_0_0%]
public ref class ViewBase abstract : public [%$TOPIC/ms617879_en-us_VS_110_3_0_2_0_0%]
type ViewBase =  
        inherit [%$TOPIC/ms617879_en-us_VS_110_3_0_3_0_1%] 

The ViewBase type exposes the following members.

Protected method ViewBaseInitializes a new instance of the ViewBase class.
Protected property DefaultStyleKeyGets the object that is associated with the style for the view mode.
Public property DependencyObjectTypeGets the DependencyObjectType that wraps the CLR type of this instance.  (Inherited from DependencyObject.)
Public property DispatcherGets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public property IsSealedGets a value that indicates whether this instance is currently sealed (read-only). (Inherited from DependencyObject.)
Protected property ItemContainerDefaultStyleKeyGets the style to use for the items in the view mode.
Public method CheckAccessDetermines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Protected method ClearItemRemoves all bindings and styling that are set for an item.
Public method ClearValue(DependencyProperty)Clears the local value of a property. The property to be cleared is specified by a DependencyProperty identifier. (Inherited from DependencyObject.)
Public method ClearValue(DependencyPropertyKey)Clears the local value of a read-only property. The property to be cleared is specified by a DependencyPropertyKey. (Inherited from DependencyObject.)
Public method CoerceValueCoerces the value of the specified dependency property. This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject. (Inherited from DependencyObject.)
Public method EqualsDetermines whether a provided DependencyObject is equivalent to the current DependencyObject. (Inherited from DependencyObject.)
Protected method FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method GetAutomationPeerIs called when a ListView control creates a ListViewAutomationPeer for its View.
Public method GetHashCodeGets a hash code for this DependencyObject. (Inherited from DependencyObject.)
Public method GetLocalValueEnumeratorCreates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject. (Inherited from DependencyObject.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method GetValueReturns the current effective value of a dependency property on this instance of a DependencyObject. (Inherited from DependencyObject.)
Public method InvalidatePropertyRe-evaluates the effective value for the specified dependency property (Inherited from DependencyObject.)
Protected method MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnPropertyChangedInvoked whenever the effective value of any dependency property on this DependencyObject has been updated. The specific dependency property that changed is reported in the event data. (Inherited from DependencyObject.)
Protected method PrepareItemPrepares an item in the view for display, by setting bindings and styles.
Public method ReadLocalValueReturns the local value of a dependency property, if it exists. (Inherited from DependencyObject.)
Public method SetCurrentValueSets the value of a dependency property without changing its value source. (Inherited from DependencyObject.)
Public method SetValue(DependencyProperty, Object)Sets the local value of a dependency property, specified by its dependency property identifier. (Inherited from DependencyObject.)
Public method SetValue(DependencyPropertyKey, Object)Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property. (Inherited from DependencyObject.)
Protected method ShouldSerializePropertyReturns a value that indicates whether serialization processes should serialize the value for the provided dependency property. (Inherited from DependencyObject.)
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Public method VerifyAccessEnforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)

The GridView view mode is derived from the ViewBase class. You typically use a GridView with the ListView control to lay out data in columns. You can also use the ViewBase class to create custom view modes for a ListView.

The View property defines the view mode that the ListView control uses.


This example shows how to create a custom View mode for a ListView control.

You must use the ViewBase class when you create a custom view for the ListView control. The following example shows a view mode that is called PlainView, which is derived from the ViewBase class.

	Public Class PlainView
		Inherits ViewBase

	  Public Shared ReadOnly ItemContainerStyleProperty As DependencyProperty = ItemsControl.ItemContainerStyleProperty.AddOwner(GetType(PlainView))

	  Public Property ItemContainerStyle() As Style
			  Return CType(GetValue(ItemContainerStyleProperty), Style)
		  End Get
		  Set(ByVal value As Style)
			  SetValue(ItemContainerStyleProperty, value)
		  End Set
	  End Property

	  Public Shared ReadOnly ItemTemplateProperty As DependencyProperty = ItemsControl.ItemTemplateProperty.AddOwner(GetType(PlainView))

	  Public Property ItemTemplate() As DataTemplate
			  Return CType(GetValue(ItemTemplateProperty), DataTemplate)
		  End Get
		  Set(ByVal value As DataTemplate)
			  SetValue(ItemTemplateProperty, value)
		  End Set
	  End Property

	  Public Shared ReadOnly ItemWidthProperty As DependencyProperty = WrapPanel.ItemWidthProperty.AddOwner(GetType(PlainView))

	  Public Property ItemWidth() As Double
			  Return CDbl(GetValue(ItemWidthProperty))
		  End Get
		  Set(ByVal value As Double)
			  SetValue(ItemWidthProperty, value)
		  End Set
	  End Property

	  Public Shared ReadOnly ItemHeightProperty As DependencyProperty = WrapPanel.ItemHeightProperty.AddOwner(GetType(PlainView))

	  Public Property ItemHeight() As Double
			  Return CDbl(GetValue(ItemHeightProperty))
		  End Get
		  Set(ByVal value As Double)
			  SetValue(ItemHeightProperty, value)
		  End Set
	  End Property

	  Protected Overrides ReadOnly Property DefaultStyleKey() As Object
			Return New ComponentResourceKey(Me.GetType(), "myPlainViewDSK")
		  End Get
	  End Property

	End Class
public class PlainView : ViewBase

  public static readonly DependencyProperty 
    ItemContainerStyleProperty =

  public Style ItemContainerStyle
      get { return (Style)GetValue(ItemContainerStyleProperty); }
      set { SetValue(ItemContainerStyleProperty, value); }

  public static readonly DependencyProperty ItemTemplateProperty =

  public DataTemplate ItemTemplate
      get { return (DataTemplate)GetValue(ItemTemplateProperty); }
      set { SetValue(ItemTemplateProperty, value); }

  public static readonly DependencyProperty ItemWidthProperty =

  public double ItemWidth
      get { return (double)GetValue(ItemWidthProperty); }           
      set { SetValue(ItemWidthProperty, value); }

  public static readonly DependencyProperty ItemHeightProperty =

  public double ItemHeight
      get { return (double)GetValue(ItemHeightProperty); }
      set { SetValue(ItemHeightProperty, value); }

  protected override object DefaultStyleKey
        return new ComponentResourceKey(GetType(), "myPlainViewDSK"); 


To apply a style to the custom view, use the Style class. The following example defines a Style for the PlainView view mode. In the previous example, this style is set as the value of the DefaultStyleKey property that is defined for PlainView.

<Style x:Key="{ComponentResourceKey 
      TypeInTargetAssembly={x:Type l:PlainView},
       TargetType="{x:Type ListView}" 
       BasedOn="{StaticResource {x:Type ListBox}}"
  <Setter Property="HorizontalContentAlignment"
  <Setter Property="ItemContainerStyle" 
          Value="{Binding (ListView.View).ItemContainerStyle,
          RelativeSource={RelativeSource Self}}"/>
  <Setter Property="ItemTemplate" 
          Value="{Binding (ListView.View).ItemTemplate,
          RelativeSource={RelativeSource Self}}"/>
  <Setter Property="ItemsPanel">
        <WrapPanel Width="{Binding (FrameworkElement.ActualWidth),
                   ItemWidth="{Binding (ListView.View).ItemWidth,
                   RelativeSource={RelativeSource AncestorType=ListView}}"
                   MinWidth="{Binding (ListView.View).ItemWidth,
                   RelativeSource={RelativeSource AncestorType=ListView}}"
                   ItemHeight="{Binding (ListView.View).ItemHeight,
                   RelativeSource={RelativeSource AncestorType=ListView}}"/>

To define the layout of data in a custom view mode, define a DataTemplate object. The following example defines a DataTemplate that can be used to display data in the PlainView view mode.

<DataTemplate x:Key="centralTile">
  <StackPanel Height="100" Width="90">
    <Grid Width="70" Height="70" HorizontalAlignment="Center">
      <Image Source="{Binding XPath=@Image}" Margin="6,6,6,9"/>
    <TextBlock Text="{Binding XPath=@Name}" FontSize="13" 
               HorizontalAlignment="Center" Margin="0,0,0,1" />
    <TextBlock Text="{Binding XPath=@Type}" FontSize="9" 
               HorizontalAlignment="Center" Margin="0,0,0,1" />

The following example shows how to define a ResourceKey for the PlainView view mode that uses the DataTemplate that is defined in the previous example.

<l:PlainView x:Key="tileView" 
             ItemTemplate="{StaticResource centralTile}" 

A ListView control can use a custom view if you set the View property to the resource key. The following example shows how to specify PlainView as the view mode for a ListView.

				'Set the ListView View property to the tileView custom view
				lv.View = TryCast(lv.FindResource("tileView"), ViewBase)
//Set the ListView View property to the tileView custom view
lv.View = lv.FindResource("tileView") as ViewBase;

For the complete sample, see ListView with Multiple Views Sample.

Version Information

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.