views:

40

answers:

1

I am trying to implement custom views that are specific to an application without any luck. Here is my problem: I need a custom view because I would like for the user to be able to switch views dynamically at runtime. I need a custom view (as opposed to only datatemplates) because the listview layout has to change as well as the Control template and the data template. All of the turorials say to implement Custom classes that derive from viewbase and override the DefaultStyleKey and ItemContainerDefaultStyleKey to return a ComponentResourceKey defined in generic.xaml. However, the problem is that I am trying to create several views that are very specific to that application. certain brushes and fonts will be consistant accross the application and the custom views will use these. i.e. I have application level Forebrush, Shadowbrush, Deepshadowbrush, TextDecorator, etc. and I want the views to use these. If the view will be defined in an external generic.xaml it will be very convoluted markup to bind to these. And besides, it would make them application specific anyway (if they bind to these brushes). Anyone have an idea how to define styles for views internaly in the application that will be able to be changed at runtime?

A: 

I'm slightly confused on your details, however you can set the Style of a ListView at runtime as such...where CustomStyle is a predefined style that you want to apply to the ListView.

ListView view = new ListView();
view.Style = CustomStyle;

The DefaultStyleKey is applicable to a custom Control (this is different then a UserControl). So say you want a new Control called a Widget. You would need ot define the DefaultStyleKey for that Widget since it does not have a default style defined. A UserControl is a collection of Controls, therefore it does not have a pre-defined style as such.

In addition you can create a ResourceDictionary to break apart your styles. You can then merge them via the App.xaml as such...

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/Generic.xaml"/>
                <ResourceDictionary Source="Themes/ListViewStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
Aaron
I'm probably not explaining myself very well. I need custom Views (derive from ViewBase) Because I need to change almost everything in the view (not the listbox) such as selection events, and the panel. I might be able to set these individually on the listview instead of supplying a custom view, but this doesnt give me the logical grouping required. I want each view should have their own layout container, selection events, animations, containers, and finally datatemplates. I cant use an external dictionary (generic.xaml) as described above
Fragilerus
So the root of the problem is how do you get the default styles for your custom control types without making use of Generic.xaml?
Aaron