Reputation: 538
I have a assembly where I define several customs controls (based on existing control) with their own style.
For all of them I define the static constructor where I set DefaultStyleKeyProperty and add the style XAML file to Themes/Generic.xaml.
It's working fine for all of them except for my custom ListView.. It's driving me crazy !
Here is a short sample :
public class EmListView : ListView
{
/// <summary>
/// Constructor
/// </summary>
static EmListView()
{
// Set the default style type
DefaultStyleKeyProperty.OverrideMetadata(typeof(EmListView), new FrameworkPropertyMetadata(typeof(EmListView)));
}
}
XAML file declared in Generic.xaml :
<Style TargetType="{x:Type ui:EmListView}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ui:EmListView}">
<Border Name="Border" Style="{DynamicResource EMLV_ListViewBorderStyle}" Margin="50">
<ScrollViewer Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
<ItemsPresenter />
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.3" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I don't know why but the only way to get my style applied on my EmListView is to force the style when using it, like :
<ui:EmListView Margin="5" Style="{DynamicResource {x:Type ui:EmListView}}">
I have EmWindow, EmButton, .. All are applying the style automatically except that ListView. Is there something special with ListView ?
Thank you.
EDIT :
I spotted something, it seem that is the declaration of my EmListView that cause the trouble.
Here is a sample declaration inside a window :
<ui:EmListView Margin="5">
<ui:EmListView.View>
<GridView>
<ui:EmSortableGridViewColumn Width="100" DisplayMemberBinding="{Binding artCode, Mode=OneWay}" SortPropertyName="artCode" Header="Code" IsDefaultSortColumn="True" />
<ui:EmSortableGridViewColumn Width="100" DisplayMemberBinding="{Binding artCode, Mode=OneWay}" SortPropertyName="artCode" Header="Code" IsDefaultSortColumn="True" />
<ui:EmSortableGridViewColumn Width="100" DisplayMemberBinding="{Binding artCode, Mode=OneWay}" SortPropertyName="artCode" Header="Code" IsDefaultSortColumn="True" />
<ui:EmSortableGridViewColumn Width="100" DisplayMemberBinding="{Binding artCode, Mode=OneWay}" SortPropertyName="artCode" Header="Code" IsDefaultSortColumn="True" />
</GridView>
</ui:EmListView.View>
</ui:EmListView>
The style doesn't apply and it constantly throw me a warning saying that it cannot define OverridesDefaultStyle property in the style.
If I simply declare it like this :
<ui:EmListView Margin="5" />
I have no warning and my style is set correctly.
Upvotes: 1
Views: 235
Reputation: 538
I solved my issue, I did a crappy workaround in my EmListView constructor as follow :
public EmListView()
{
Style defaultStyle = (Style)Application.Current.TryFindResource(typeof(EmListView));
if (defaultStyle != null)
{
this.OverridesDefaultStyle = true;
this.Style = defaultStyle;
}
}
If someone can tell me why I have to do this trick only for my custom ListView control I will be happy to know.
Upvotes: 1