Using ItemContainerStyle in ListView.

ItemContainerStyle in Listview



ItemContainerStyle is used to change the appearance of default ListViewItem.

The below code snippet gives the usage of itemcontainerstyle and control template.


<Window x:Class="WpfApplication1.Window5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window5" Height="300" Width="300">
<Grid Name="grid">
<ListView ItemsSource="{Binding}" BorderThickness="0" Name="listview">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=age}" Width="100" Grid.Row="0" Grid.Column="0"></TextBlock>
<TextBlock Text="{Binding Path=name}" Width="100" Grid.Row="0" Grid.Column="1"></TextBlock>
<Expander IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
<Expander.Header>
Status
</Expander.Header>
<Expander.Content>
<TextBlock Text="{Binding Path=address}"></TextBlock>
</Expander.Content>
</Expander>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Age" Width="100"></GridViewColumn>
<GridViewColumn Header="Name" Width="100"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>

</Grid>
</Window>


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: