Default Style Not Applying
-
Wednesday, February 06, 2008 3:30 PM
I'm creating a ListViewEx control based off the ListView Control. The Code works but the Default Style I made for it does not work when I run the app (though it shows in the VS 2008 designer). Instead I get the normal ListView Style.
It will work if I do this though:
Code SnippetCode SnippetStyle style = (Style)Application.Current.FindResource(typeof(ListViewEx));
lv.Style = style;
It's not taking the Default Style on its own and I don't know why.
Here's the VS 2008 Solution. I've never made a control library before. Any help fixing this would be appreciated.
All Replies
-
Wednesday, February 06, 2008 6:18 PM
Hi Emanon,
your style is applying. The problem is located in the GridView. :-)
Just set your Listbox like this without specifying the gridView:
Code Snippet<
ctrl:ListViewEx Name="lv" Margin="10"/>
You'll see, that your template applies. Let's get further and write a custom View for your ListBox. :-) -
Wednesday, February 06, 2008 7:33 PM
Yeah, I have noticed not setting a view allows the style to show. But it doesn't make sense to me that setting a view would effect the style of its parent control. It also doesn't make sense to me that
Code SnippetStyle
style = (Style)Application.Current.FindResource(typeof(ListViewEx));lv.Style = style;
...will have the style of the ListViewEx show up despite having put in a GridView. It is also strange that the style shows up in the designer including the GridView's column headers w/ my style, but doesn't when the app is run. I would like to use the GridView. It already does what I'd like it to. Why would I want to rewrite it? I'd just like to have my style apply to my derived ListView control and still have it be compatible w/ the GridView.
Do you know how to do this?
-
Wednesday, February 06, 2008 9:16 PM
The problem is, that setting a View overrides the Style of the ListView. So you have to re-set the style explicit.
Below some code out of the ListView-Class (Just debugged with microsofts symbols, really cool. :-))
There you see that the DefaultStyleKey of the ListView is set to the DefaultStyleKey specified in the GridView.
Code Snippetprivate void ApplyNewView()
{
ViewBase newView = View;
if (newView != null)
{
// update default style key of ListView
DefaultStyleKey = newView.DefaultStyleKey;
}
...
}
So what to do... you could create a subclass from GridView that only overrides the DefaultStyleKey-Property:
Code Snippetpublic class MyGridView:GridView
{
protected override object DefaultStyleKey{
get{
return typeof(ListViewEx);}
}
}
Then it'll work, if you use the MyGridView:
Code Snippet<ctrl:ListViewEx Name="lv" Margin="10">
<ctrl:ListViewEx.View> <ctrl:MyGridView> <ctrl:SortableGVC Header="FirstName" SortPropertyName="FirstName" DisplayMemberBinding="{Binding Path=FirstName}" IsDefaultSortColumn="True"/> <ctrl:SortableGVC Header="LastName" SortPropertyName="LastName" DisplayMemberBinding="{Binding Path=LastName}"/> </ctrl:MyGridView> </ctrl:ListViewEx.View> </ctrl:ListViewEx> -
Thursday, February 07, 2008 1:48 PM
Ahh, I see you are right. That's a great idea and it works really well. Thank you very much !
Another thing I had tried to just get the headerStyle to work was :
Code Snippetpublic GridViewEx()
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri("Themes/ListViewEx.xaml", UriKind.Relative));
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
ResourceDictionary dic = (ResourceDictionary)reader.LoadAsync(sri.Stream);
this.ColumnHeaderContainerStyle = (Style)dic["ListViewExGridViewColumnHeader"];
}
It didn't work. I'm doing it wrong, I think. I was just curious, is it possible to have something like a GridView which is not a control pull up a Style from a xaml file in code behind and assign it to one of its properties like I was trying to do here?

