WPF CustomControl - Resources Property - wpf

I have CustomControl deriving from another control in which I set the default styles in its resources property (in xaml). Later, I want to use this CustomControl and add new resources to it by using the default syntax: <my:CustomControl.Resources>...
How can I do this? Currently, I get an error: Cannot re-initialize ResourceDictionary instance.

You have tried with merge?
http://msdn.microsoft.com/en-us/library/aa350178.aspx
[EDIT]
The link above isn't working anymore...
This is the right answer:
Merged ResourceDictionary vs App.xaml

There is couple solutions:
Add new resources to parent's resource block.
Add new resources as resource dictionary.

Related

Define Style in ResourceDictionary, but EventSetter locally

I have restyled a DataGrid and it got rather complex so I moved the styles (DataGrid, DataGridCell, DataGridRow,...) to a ResourceDictionary. I gave all of them a x:Key and referenced them e.g. as DataGrid.RowStyle="{StaticResource SuperRowStyle}". Now I want to use an EventSetter on the Row as well, but obviously I can't set it in the ResourceDictionary, but I can't set it in the Window as well, since it gets overridden by the RowStyle property. How can I do this?
BR,
Daniel
I'll answer it myself as a good solution is provided in the comments. I create a style in the window where I need the EventSetter and use BasedOn with the needed style.
Now I want to use an EventSetter on the Row as well, but obviously I can't set it in the ResourceDictionary ..
Yes, you can provided that you add a code-behind file to the ResourceDictionary:
Is it possible to set code behind a resource dictionary in WPF for event handling?
The handler must be defined in the same class as the EventSetter.

ListView Custom Control Template reset when adding view

The control resets to the standard ListView in the designer when I declare a view in XAML.
<my1:CustomControl1.View>
<GridView></GridView>
</my1:CustomControl1.View>
I tried doing the same thing in code. The CustomControl1.Template changes from having a TargetType of "CustomControl1" to the default ListView template after I set the view to a new GridView().
The template itself doesn't seem to be the problem - it works if I use it as a local resource. If I can't get this working I could make a UserControl with a templated listview inside but for various reasons I'd like a CustomControl. Any help appreciated.
I found another solution here. You create your own GridView class and have it return the custom control as the default style key. You then use this instead of the standard gridview
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/056df061-0666-4fe2-9fa6-8a6440a23ff0/

WPF: Create a custom control without rewriting the ControlTemplate

Hey, I am creating a Custom Control i WPF inheriting from the ListView. However, I want it to look exactly as the already existing ListView.
Is there a way To use the default ListView Template in a Custom Control without rewriting it in xaml? I do have a Generic.xaml file with the new control added, but I should no need to rewrite the template code.
Thanks
EDIT: I also want to keep it as DRY as possible without repeating (making a mess) the code.
If you subclass the ListView, them your subclassed control will use the ListView Template. That's it! You do not have to do anything!
The Template used by a control is defined by its DefaultStyleKey dependency property. If you want to change the template of your control, set this property as follows:
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl),
new FrameworkPropertyMetadata(typeof(MyControl)));
However, if you do not set this property, it will use the value set by the superclass.
I think the problem is that you have used "Add New Item" => "Custom Control" to create you control then changed the class it extends. Instead of doing this, just add a new C# class and extend ListView.
<Style TargetType="{x:Type local:MyControl}" BasedOn={StaticResource {x:Type ListView}}" />

WPF custom View without Generic.xaml

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?
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>

WPF code-behind for resources?

I'm working on a wpf application, and up until recently, I had a ResourceDictionary inside my main window's resources part of the xaml. The resource dictionary contained an DataTemplate that was used to style several listboxes in the window. The xaml for this datatemplate contained pointers to event handlers, eg:
<Button n:Name="btnClickMe" Content="Click Me!" LeftMouseButtonUp="btnClickMe_Click" />
I recently decided to split the content of the window up into separate user controls, and to move my ResourceDictionary into it's own file. But, of course, there isn't a code-behind file for a resource dictionary file. How can I wire this up, with things split up as I've described?
Thanks in advance!
You can add a code-behind to a ResourceDictionary; just make sure your class names are referenced correctly. For instance, in the ResourceDictionary if you were working with AppStyles.xaml the XAML file would have a class of:
x:Class="Client.App.Shell.themes.AppStyles"
In the code-behind, AppStyles.xaml.cs, you would make sure to have the class:
namespace Client.App.Shell.themes
{
public partial class AppStyles
...
You can add a new class and name it with the same name as your resource dictionary plus the .cs extension and Visual Studio will automatically set things up so it becomes the code behind file.
For example if you have a resource dictionary called Buttons.xaml, add a file called Buttons.xaml.cs.
You should consider using RoutedCommands, I am thinking.
there are many many resources online, here are a couple that might help you.
http://msdn.microsoft.com/en-us/library/ms752308.aspx
http://www.devx.com/DevX/Article/37893/0/page/1

Resources