A ListView
let you define a set of views
for it and gives you a native way (WPF
binding
support) to control the display of ListView
by using defined views
.
Example:
XAML
<ListView ItemsSource="{Binding list}" Name="listv" MouseEnter="listv_MouseEnter" MouseLeave="listv_MouseLeave">
<ListView.Resources>
<GridView x:Key="one">
<GridViewColumn Header="ID" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding id}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
<GridView x:Key="two">
<GridViewColumn Header="Name" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.Resources>
<ListView.Style>
<Style TargetType="ListView">
<Style.Triggers>
<DataTrigger Binding="{Binding ViewType}" Value="1">
<Setter Property="View" Value="{StaticResource one}" />
</DataTrigger>
</Style.Triggers>
<Setter Property="View" Value="{StaticResource two}" />
</Style>
</ListView.Style>
Code Behind:
private int viewType;
public int ViewType
{
get { return viewType; }
set
{
viewType = value;
UpdateProperty("ViewType");
}
}
private void listv_MouseEnter(object sender, MouseEventArgs e)
{
ViewType = 1;
}
private void listv_MouseLeave(object sender, MouseEventArgs e)
{
ViewType = 2;
}
OUTPUT:
Normal View: View 2 in above XAML
MouseOver View: View 1 in above XAML
If you try to achieve above in a
ListBox
, probably you'll end up writing a lot more code forControlTempalate
/ItemTemplate
ofListBox
.