I am designing styles for WPF ListView. I am facing some issues while using ListViewItem and GridViewColumn inside ListView. Please refer the code mentioned below of ListViewItem style:
<Style x:Key="{x:Type ListViewItem}" TargetType="ListViewItem"><Setter Property="SnapsToDevicePixels" Value="true" /><Setter Property="OverridesDefaultStyle" Value="true" /><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="ListBoxItem"><Grid><Border x:Name="Border" Padding="2" SnapsToDevicePixels="true" Background="Transparent"><GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /></Border></Grid></ControlTemplate></Setter.Value></Setter></Style>
The issues are listed below:
1) UseCase1: Using ListViewItem in ListView:
<ListView Name="ListView1" Width="200" Height="400"><ListViewItem Content="Coffie" ></ListViewItem><ListViewItem Content="Tea"></ListViewItem></ListView>
The above mentioned style does not work in this scenario, since, GridRowPresenter is used instead of ContentPresenter. If i replace GridRowPresenter with ContentPresenter, the ListViewItems are displayed as expected.
2) UseCase2: Using GrirViewColumn in ListView:
<ListView Width="300" Height="200"><ListView.View><GridView><GridViewColumn Header="Name" Width="120" ><GridViewColumn.CellTemplate><DataTemplate><TextBlock Text="{Binding Name}" /></DataTemplate></GridViewColumn.CellTemplate></GridViewColumn></GridView></ListView.View> </ListView>
The above mentioned style works fine for this UseCase since GridRowPresenter is present in the style.
Question: I would like to know how to create a single style which would handle both the scenarios (ListViewItem and GridViewColumn).