Reputation: 1841
I derived a class from ListViewItem, it has some custom dependency properties:
public class CustomListViewItem : ListViewItem
{
public static DependencyProperty CustomDependencyProperty;
...
}
There is also a ControlTemplate for this class.
<Style TargetType="{x:Type local:CustomListViewItem}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomListViewItem}">
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
Now I want to use this CustomListViewItem in a ListView instead of ListViewItem. But when I try to do something like:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type local:CustomListViewItem}">
...
</Style>
</ListView.ItemContainerStyle>
compiler says: "A style intended for type 'CustomItem' cannot be applied to type 'ListViewItem".
I know that I can use ControlTemplate with ListViewItem TargetType to customize ItemContainerStyle or DataTemplate to customize ItemTemplate, but how can I subclass ListViewItem to substitute my own Item type?
Any help will be appreciated.
Upvotes: 1
Views: 1326
Reputation: 1841
I found the answer after considering this question. The core idea is that it is necessary to create not only a custom ListViewItem, but also a custom ListView and override GetContainerForItemOverride() method for it:
public class CustomListView : ListView
{
static CustomListView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomListView), new FrameworkPropertyMetadata(typeof(CustomListView)));
}
protected override DependencyObject GetContainerForItemOverride()
{
return new CustomListViewItem();
}
}
Of course, it's also necessary to provide a proper ControlTemplate for a CustomListView.
Also PrepareContainerForItemOverride
method will be useful.
Upvotes: 3