Define Style in ResourceDictionary, but EventSetter locally - wpf

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.

Related

How can I base a style on a default control's style?

We're writing a custom subclass of TextBox where we need to change only some basic parts of the style. Now we know when you subclass Control, normally you're supposed to change the metadata like this...
public class EnhancedTextBox : TextBox {
static EnhancedTextBox() {
// Commenting this line out lets this use the default TextBox style.
DefaultStyleKeyProperty.OverrideMetadata(
typeof(EnhancedTextBox),
new FrameworkPropertyMetadata(typeof(EnhancedTextBox)));
}
}
This changes the key used for that subclass type to be the type itself, which means it no longer gets the default TextBox style. No biggie... we just comment that line out and it again works since the key is still set to the value used by TextBox directly.
However, we're wondering if we did want to change a few things in the style, but not the entire style, we could simply create a new style and set its BasedOn property... but what do we set there? We don't want to have to pull out the XAML manually and create a new style, give it a key, then use a StaticResource, but we haven't found out what we could set there to say 'This is based on the TextBox style.
I'm hoping its something simple but again, we haven't found it. Can anyone help?
And just like that... I found it. Man, kicking myself that it was this obvious and I missed it!
<Style TargetType="{x:Type local:EnhancedTextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
....
</Style>
Found it here... WPF style basedon current

how to use style defined in xaml resource dictionary?

I have a a IMultiValueConverter that dynamically creates TextBlock controls. The issue is that it has no styles.
How can I tell my new TextBlock to use a style that was defined in my XAML resource dictionary?
See the following question: how to use DynamicResource in the code behind?
Use SetResourceReference, it's equivalent to use DynamicResource in Xaml
So if your Style has the Key myTextBlockStyle
TextBlock textBlock = new TextBlock();
textBlock.SetResourceReference(FrameworkElement.StyleProperty, "myTextBlockStyle");
I have never tried this before, and depending on what your converter is doing, I think if your XAML resource dictionary is external, then link it into the Window where you are displaying the TextBlocks:
<Window.Resources>
<ResourceDictionary Source="[the path to the resource dictionary]"/>
</Window.Resources>
Then in your textblocks, ensure they have the Style attached that is defined in the resource dictionary. If the textblocks are being created in code behind I believe you should be able to use FindResource to locate the style that is linked in by the resource dictionary. Then do something like this:
textBlock1.Style = (Style)FindResource("myTextBlockStyle");

WPF CustomControl - Resources Property

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.

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}}" />

Silverlight - preventing ChildWindow movement

Anyone got any neat solutions to prevent a Silverlight ChildWindow being moved?
thanks,
Mark
I'm not sure you'd call this neat but...
Create yourself a new Templated control and call it ImmovableChildWindow.
Modify the class it inherits from to be ChildWindow.
Open Themes/generic.xaml you will find an initial style for the ImmoveableChildWindow
In the Silverlight documentation you'll find the existing template for a ChildWindow at ChildWindow Styles and Templates.
Note the existing TargetType value for the ImmovableChildWindow style.
Copy'n' paste the whole default style for a ChildWindow from the documentation into your themes/generic.xaml file.
Replace TargetType for this copy to the same value as the exiting ImmovaleChildWindow style.
You can now delete the initial style. Leave only the large copy of ChildWindow style now targeting ImmovableChildWindow.
Find within the Template setter change the TargetType of to the same value as the style TargetType
Search through the template and find a Border with the name Chrome. Delete the x:Name="Chrome" attribute. (This is what we are really after).
Now when you create a new ChildWindow item it will by default inherit form ChildWindow, if you want it to be immovable you need modify it to inherit from ImmovableChildWindow instead (change the base type in the code-behind and the root tag name in the xaml).
The ChildWindow attaches events to the FrameWorkElement with the name "Chrome" which enables the child window to be moved about. However being a well-behaved templated control, if it can't find a FrameworkElement called "Chrome" it just continues to work without that feature.
Not Required to Create new class, instead
Copy the style from: http://msdn.microsoft.com/en-us/library/dd833070%28VS.95%29.aspx
Give x:key="stylename"
In Construtor of Childwindow, paste following code before InitializeComponent:
this.Style = App.Current.Resources["childWindow"] as Style;
above solution resolved my issue
Maybe you can try this simple way to do that:
Create a Grid to warp all the content in your ChildWindow.
<Grid Margin="0">
<!--Your ChildWindow. Canvas, Grid, Textblock...Whatever-->
</Grid>
Since the Grid has a 0 margin, you can not click it and move it.

Resources