Wpf Custom Popup control and application theming: Resources not found - c#

I have an app where I set a theme (ResourceDictionary) on the main Window. Then all children "inherits" styles, themes etc. via the DynamicResourceExtension. This works.
However, i have a custom Popup control to handle long press animation on buttons:
class LongPressAnimationControl : Popup
{
static LongPressAnimationControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LongPressAnimationControl),
new FrameworkPropertyMetadata(typeof(LongPressAnimationControl)));
}
/* Various DP's, events, properties, methods ETC. */
}
with a default style in Generic.xaml:
<Style TargetType="{x:Type controls:LongPressAnimationControl}">
<Setter Property="Delay" Value="00:00:00.500" />
<Setter Property="Duration" Value="00:00:01.000" />
<Setter Property="Image" Value="{StaticResource Image.LongPress}" />
<Setter Property="EasingFunction"><Setter.Value><CubicEase/></Setter.Value></Setter>
</Style>
This works. the properties change to the ones set in the style.
However, i would like the popup to use a style defined in the Theme.xaml so the properties etc. can be themed.
If i move the style to my Theme.xaml, then the style is not picked up (properties does not change).
I've also tried to add a key to the Style, and a resource reference in code, like so:
public LongPressAnimationControl()
{
SetResourceReference(StyleProperty, "LongPressAnimationControlStyle");
//....
}
Anyone with an idea of how to solve this?

You should include Theme.xaml in App.xaml resource, so WPF can find the resource.

Related

How to apply style in WPF to all controls without overriding their original styles?

I want to create a style, which will normalize margins for all controls in a specific scenario:
<Style TargetType="FrameworkElement" x:Key="MyStyle">
<Setter Property="Margin" Value="{StaticResource DialogItemsExceptTopMargin}" />
</Style>
I have then a couple of different controls: textboxes, comboboxes, checkboxes etc., to which I want to apply this style.
However, when I do that, their look is immediately reverted to the platform style (I am applying a style from 3rd party library). How can I define my style so that the original styles are kept intact?
Mind: I know, that I can use BasedOn for specific type:
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
...
</Style>
This however would require me to create completely separate styles for all possible controls, which I may use. I tried this trick with FrameworkElement and Control, but I got an error, that there is no such StaticResource.

WPF window style applied to wrong windows

I have created custom window classes for dialogs like this:
public class DialogBase : Window
{
// common stuff here
}
public class DialogOkCancel : DialogBase
{
static DialogOkCancel()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogOkCancel),
new FrameworkPropertyMetadata(typeof(DialogOkCancel)));
}
// specific stuff here
}
The styles looks like this, and contains dialog specific things such as title bar, OK/Cancel buttons etc.
<Style x:Key="DialogBaseStyle" TargetType="{x:Type base:DialogBase}" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="ShowInTaskbar" Value="False"/>
<!-- more stuff here -->
</Style>
<Style TargetType="{x:Type base:DialogOkCancel}" BasedOn="{StaticResource DialogBaseStyle}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type base:DialogOkCancel}">
<!-- more stuff here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This works perfectly fine during runtime. Also, the dialogs displays fine in XAML Designer.
However, in XAML Designer, the DialogOkCancel style gets applied to other all other windows as well, such as the application main window (which simply derives from plain Window). The title bar, buttons and everything. I don't really get this, as the TargetType is specific. This does not occur at runtime.
What am I not seeing here ?
Correct way to apply a style both while in designer mode and during runtime is to have declared the style in App.xaml. Best practice would be to have a MergedDictionaries, containing a reference of your various style dictionaries, which will be in other files.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Converters -->
<ResourceDictionary Source="Resources/Converters.xaml"/>
<!-- Windows -->
<ResourceDictionary Source="Resources/WindowStyles.xaml"/>
// ... and all others //
WindowStyles.xaml can contain the exact styles you wrote above. They will be applied to all DialogOkCancel windows, both in designer and runtime.
Hope that helped.

WPF - Turn on/off keyboard from code behind

There's a custom touchscreen keyboard in my app built according to this: https://www.codeproject.com/Articles/32568/A-Touch-Screen-Keyboard-Control-in-WPF.
I also have a ResourceDictionary containing all styles and templates. In the TextBox style, I can set the keyboard ON/OFF:
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="14" />
<Setter Property="Padding" Value="4" />
<Setter Property="k:TouchScreenKeyboard.TouchScreenKeyboard" Value="True"/>
</Style>
I would like the user to be able to turn it on or off from the UI, but can't figure out how to reach this property from code behind. I would like to make it without naming the style, since it's pretty commonly used throughout the app.
I tried this, but (no surprise) get ArgumentNotFoundException:
Style s = Application.Current.FindResource("defTextBox") as Style;
s.RegisterName("Keyboard.TouchScreenKeyboard.TouchScreenKeyBoard",false);
Any help would be appreciated!
You shoud be able to set the TouchScreenKeyboard attached property for an individual TextBox like this:
TouchScreenKeyboard.SetTouchScreenKeyboard(textBox1, false);
Changing the defintion of the implicit Style itself after it has already been applied to all TextBox elements doesn't make much sense though. You should define the default value in XAML and then change the value for individual TextBoxes dynamically at runtime if you need to.

Disable Style in WPF XAML?

Is there anyway to turn off a style programatically?
As an example, I have a style that is linked to all textboxes
<Style TargetType="{x:Type TextBox}">
I would like to add some code to actually stop the style elements being used, so basically reverting back to the default control style.
I need a way to make a switch in my styles, so I can switch between Windows default style and my custom style through C# code.
Is there anyway to do this?
Thanks
Working Solution
Switching between themes in WPF
For setting the style to default,
In XAMl use,
<TextBox Style="{x:Null}" />
In C# use,
myTextBox.Style = null;
If style needs to be set as null for multiple resources, see CodeNaked's response.
I feel, all the additional info should be in your question and not in the comments. Anyways, In code Behind I think this is what you are trying to achieve:
Style myStyle = (Style)Application.Current.Resources["myStyleName"];
public void SetDefaultStyle()
{
if(Application.Current.Resources.Contains(typeof(TextBox)))
Application.Current.Resources.Remove(typeof(TextBox));
Application.Current.Resources.Add(typeof(TextBox),
new Style() { TargetType = typeof(TextBox) });
}
public void SetCustomStyle()
{
if (Application.Current.Resources.Contains(typeof(TextBox)))
Application.Current.Resources.Remove(typeof(TextBox));
Application.Current.Resources.Add(typeof(TextBox),
myStyle);
}
You could inject a blank Style that would take precedence over your other Style. Like so:
<Window>
<Window.Resources>
<Style TargetType="TextBox">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<Grid.Resources>
<Style TargetType="TextBox" />
</Grid.Resources>
</Grid>
</Window>
In the example above, only the Grid's implicit Style would be applied to TextBoxes in the Grid. You could even add this to the Grid programmatically, something like:
this.grid.Resources.Add(typeof(TextBox), new Style() { TargetType = typeof(TextBox) });
I know the answer has been accepted, but i want to add my solution which works awesome in the following scenario:
One main application using mahapps.metro
additional project imported from the main application with no reference to mahapps.metro, it is imported as a plugin (loading compiled .dll on the fly)
using the < ToolBar> re-styles everything to null therefore the mahapps.metro styles are not being applied to items inside the toolbar.
usercontrol is used to provide custom controls to the main application.
in the user control root set the resources:
<UserControl.Resources>
<Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" />
<Style x:Key="ComboBoxStyle" TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}" />
</UserControl.Resources>
then the toolbar code can be the following
<ToolBar>
Block Template:
<ComboBox Style="{StaticResource ComboBoxStyle}"/>
<Button Content="Generate!" Style="{StaticResource ButtonStyle}"/>
</ToolBar>
this successfully applies the main application style to the controls inside the < ToolBar>
In Xaml, you can override this by setting a style explicitly. In code-behind, you can also set the style explicitly.
<TextBox Style="{StaticResource SomeOtherStyle}"/>
myTextBox.Style = Application.Resources["SomeOtherStyle"];

How to add a trigger to a WPF custom control without overriding the existing style?

I am creating a simple custom control extending from toggle button that allows the user to specify checked and unchecked content directly in XAML. It works well but it is based on a trigger, and I don't know how to define the trigger except in a style. If I define the style, then I lose anything set outside of the custom control.
What I would like to be able to do is just append this trigger to any existing style set elsewhere on the control.
Here's the XAML for the style/trigger.
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Self}, Path=UncheckedContent}" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content"
Value="{Binding RelativeSource={RelativeSource Self}, Path=CheckedContent}" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
I tried inheriting the style via the BasedOn with a default type but it won't work if the custom control has an explicit style set by its parent. I also considered EventTriggers but I do not believe there would be an event to initialize the control.
Thanks for any help anyone can offer. :)
Just to clear things up on the terminology here: A user control is a control that derives from the UserControl class. If I understood you right you derived from ToggleButton to add the UncheckedContent and CheckedContent properties. In that case you have created a custom control. It's always easier to follow if we agree on common terminology :)
As far as I know you can not do such a generic style inheritance in XAML. You always have to specify explicitly what style a another style is based upon. Your style can either be based on the default style for ToggleButton or on a specific other style. If you can't build a style inheritance chain that respects that, this approach won't work.
But since you have a custom control, couldn't you write a default style for it that is based on the default toggle button style like this?
<Style TargetType="{x:Type CustomToggleButton}"
BasedOn="{StaticResource {x:Type ToggleButton}}">
Then whenever you apply an explicit style to a toggle button you would specify that it is based on the default toggle button style.
Also you could write a (default) control template for your new toggle button in Themes\Generic.xaml that contains the above triggers. In blend you can get a copy of the default template for toggle button ("Edit Template"->"Edit a Copy") so you can make sure that your toggle button looks exactly like the normal toggle button. Then incorporate the triggers above into that template.
BTW: you do not have to create a new control just to add new properties. You can add new properties to an existing control using attached properties. They can be used from XAML just like normal properties.

Categories

Resources