Creating style resource for custom control removes template - c#

I've got a custom control in WPF, which has a variety of dependency properties that allow visual customization. For the sake of brevity I won't post the entire control, but it basically is setup like this:
<UserControl.Resources>
<Style TargetType="{x:Type MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MyControl}">
<Border BorderBrush="{TemplateBinding BorderColor}">
// more stuff here
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter>
</Setter>
</Style>
</UserControl.Resources>
The BorderColor property works fine if I set it directly, like this:
<ctl:MyControl BorderColor="Brushes.Red">....</ctl:MyControl>
But I want to set it application-wide. The problem I have is if I simply set the style with no key, it does not apply. Like this:
<Window.Resources>
<Style TargetType="{x:Type ctl:MyControl}">
<Setter Property="BorderColor" Value="Brushes.Red"/>
</Style>
</Window.Resources>
This does not do anything to the control. So I thought I'd just set a key and apply that style, like this:
<Style TargetType="{x:type ctl:MyControl}" x:Key="myStyle">....</Style>
<ctl:MyControl Style="{StaticResource myStyle}">.....</ctl:MyControl>
But this causes the control to vanish, I'm assuming because it's removing the Template. What am I doing wrong? With other framework controls you can just set the properties you want without losing the control template.

You need to inherit from the default style you have created.
inherit style from default style

Related

convert usercontrol control to style

hi im new in wpf I have a control that has been created in user control I want to create this control in style I do not know what to do:
this is xaml codes:
https://gist.github.com/ghost1372/8b3db759241b3ddb838789e446efb0b4#file-multiselectcombo-xaml
and this is cs codes:
https://gist.github.com/ghost1372/8b3db759241b3ddb838789e446efb0b4#file-multiselectcombo-cs
Well basically all you have to do is to define a Style and set the properties using Setters. Something like:
<Style x:Key="MultiSelectCombo" TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
...
<Setter Property="Template">
<Setter.Value>
<!-- Put the control template you used in your UserControl -->
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<!-- Put the DataTemplate you used in your UserControl -->
</Setter.Value>
</Setter>
</Style>
Now this style can be applied to any ComboBox and will change its look. However if you want also change the behavior you need to create a MultiSelectCombo class that overrides ComboBox and implements the logic you want.

WPF old Control Template inside new Control Template

I use a custom control from an external library, it has a property with ControlTemplate.
<Style x:Key="{x:Type controls:HeaderDiscountButton}" TargetType="{x:Type controls:HeaderDiscountButton}" BasedOn="{StaticResource {x:Type core:ValueButton}}">
<Setter Property="core:ButtonWithPopup.PopupTemplate">
<Setter.Value>
<ControlTemplate>
some complicated xaml code
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I would like to override it, but use old one in my new ControlTemplate, for example
<Style x:Key="{x:Type controls:HeaderDiscountButton}" TargetType="{x:Type controls:HeaderDiscountButton}" BasedOn="{StaticResource {x:Type core:ValueButton}}">
<Setter Property="core:ButtonWithPopup.PopupTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Border>some complicated xaml code</Border>
<Border>some additional xaml code</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I would like to avoid copying whole old code
This is not possible I am afraid. You can indeed base a Style on another Style and override specfic setters but you cannot "override" or inherit only a part of a ControlTemplate. Unfortunately you must (re)define the entire template as a whole.
So in this case you actually need to copy the (whole) original template and then edit it as per your requirements.

C# WPF Style TreeViewItem

I made a custom resource dictionary style for a TreeViewItem, but I am having difficulties with it.
<Style x:Key="StageTreeViewItem" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="Foreground" Value="Gold"/>
<Setter Property="FontFamily" Value="ArialN"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<Grid>
<Image Name="PrimaryButtonImage" Source="pack://application:,,,/Images/TreeViewItem/TreeViewItem_Normal.png"/>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The content/header of the TreeViewItem does not exist. I put "Stage One" as Header of the TreeViewItem, but it doesn't show up. Also, if I add multiple tree view items on another, it does not expand at all.
Another thing:
How can I remove the highlights when I select the tree view item? I want it to be transparent even when I hover over it and even when I click it. I don't want anything to happen, but I just don't know how, I tried everything.
Your provided code is not making it clear how you're setting header of TreeViewItem.
For other part of the question, you can use Triggers for events happening in WPF forms. Also have a look at this link, as you'll have to define a template for changing background color on mouse hover.
IsMouseOver Trigger not working in WPF

Different tab style depending on tab item template?

I have a tab control with two different potential item templates:
<TabControl ItemTemplateSelector="{StaticResource tabTemplateSelector}"/>
Now, I also have styles for it:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
...
The thing is, I want the tab item style template to be different depending on the value of ItemTemplateSelector. How can I do this? Basically, I don't want that entire style rule to be applied to every tab item, just the ones with a specific tab item template.
Update: to make it clearer, the style has TargetType set to TabItem. I want to apply that style only to those tab items that have a specific item template.
The ContentPresenter in the ControlTemplate will display what's in the ItemTemplate. So you won't be able to switch the Template from inside the ControlTemplate.
Instead, you could use a DataTrigger to set the Template. The DataTrigger will check if the ItemTemplate that will be generated should have the other Template or not.
You will probably need a converter for this but here is an easy example. Say that your ItemTemplateSelector returns the other DataTemplate if Name is equal to "Tab 2". Then your Style with the DataTrigger would look like this
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<!-- Your first Template -->
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Name}" Value="Tab 2">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<!-- Your other Template -->
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>

When setting the background color of a Menu, the colors only appears on the top most level

All of the MenuItems do not inherit the color. Is there a way in WPF for me to set the color automatically from the main parent colors? Thanks.
I've just discovered that the Foreground property goes through all children. Background doesn't though. :(
This is happening because of the default style/template of a ContextMenu. You need to change the template of the MenuItems in your Menu to achieve the results you desire. You can do this by defining a Style with Key="{x:Type MenuItem}" in the Menu.Resources:
<Menu>
<Menu.Resources>
<Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}">
<Style Property="Template">
<Style.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<!-- your template parts here using -->
</ControlTemplate>
</Style.Value>
</Style>
</Style>
</Menu.Resources>
</Menu>

Categories

Resources