Is nested ControlTemplate possible? Control Template inside Control Template - wpf

I want to make two controlTemplate at once and invoke them separately with common properties some.
<ControlTemplate x:Key="ProcessTemplate" TargetType="{x:Type Button}">
<ControlTemplate x:Key="DecisionTemplate" TargetType="{x:Type Button}">
<!--I want to add like this-->
<Grid >
<Path Style="{StaticResource Process}" ToolTip="Process">
<ControlTemplate> <Path Style="{StaticResource Process_DragThumb}"/> </ControlTemplate>
</Path> <Viewbox> </Viewbox>
</Grid>
</ControlTemplate>
Code is shown in picture

Related

How can I add a context to ellipse button in XAML?

I'm trying to add a context to an ellipse button but I do not know how I can accomplish it.
So far all what I can do is
<Ellipse Fill="{TemplateBinding Background}"/>
but I do not want a background, I want a text. If someone can help me in here that will help alot..
Here's the code:
<Button Grid.Column="0" Height="40" Width="40" Margin="-26,-10,26,10" FontWeight="Bold">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Ellipse/>
</ControlTemplate>
</Button.Template>
<Button.Content>
Meat
</Button.Content>
</Button>
If you can help to me, please copy and past the code with the answer:
<ControlTemplate TargetType="{x:Type Button}">
<Ellipse>
<ContentPresenter/>
</Ellipse>
</Grid>

How to change the custom content of a ToggleButton in XAML

I have changed the style of a ToggleButton in a ResourceDictionary. In my style I'm changing the ControlTemplate of the ToggleButton and I place an image before the Content.
Now here is my question: The image needs to be changed for different ToggleButtons I use in XAML, should I define different styles for each ToggleButton with different image or is there some way I can change its image in XAML?
<Style x:Key="BaseToggleButton" TargetType="ToggleButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<StackPanel Orientation="Horizontal" x:Name="Border">
<Image Width="13" Height="13" Source="{StaticResource ColumnsLayoutMiniIcon}"/>
<TextBlock>
<ContentPresenter/>
</TextBlock>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Is there a way I can change the image here instead of in style?
<RadioButton Content="Plan View"
GroupName="View"
Style="{StaticResource BaseToggleButton}">
</RadioButton>
Sure, you can piggy back in on an unused property that comes in real handy for this sort of situation called Tag which you can you use to pass in your image path or resource declaration etc once we go bind it to the template like;
<Style x:Key="BaseToggleButton" TargetType="ToggleButton">
<!-- Let's give it a default -->
<Setter Property="Tag" Value="{StaticResource ColumnsLayoutMiniIcon}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<StackPanel Orientation="Horizontal" x:Name="Border">
<Image Width="13" Height="13"
Source="{TemplateBinding Tag}"/>
<ContentPresenter/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now we can either leave as is and it should display that mini icon as your default, you can specify a new one at the instance level like;
<ToggleButton Content="Plan View"
Tag="{StaticResource ADifferentImagePathOrResourcePointingToOne}"
Style="{StaticResource BaseToggleButton}"/>
Hope this helps, cheers.

WPF: Problem applying style to custom TabItem Header through ControlTemplate and ContentPresenter.Resources

I am trying to write my own control template for a TabItem Header, and have got the basic layout to work but now I wish to apply styling to the content of the Header, for example to manipulate the size and font of a textblock.
In order to test this, I have put an ellipse in the tabitem header and am attempting to fill that ellipse with the Gold brush through styling. However, it is not working. The ellipse is present, and the control template is being applied, but the fill of the ellipse is not Gold. The style within the ContentPresenter.Resources is being ignored (and Resharper has even greyed it out to prove that). Any ideas what I'm doing wrong? Thanks.
Here is the code:
<TabItem>
<TabItem.Template>
<ControlTemplate x:Name="theTabItemControlTemplate" TargetType="{x:Type TabItem}">
<Border BorderBrush="DarkBlue" BorderThickness="10">
<Grid>
<ContentPresenter ContentSource="Header" RecognizesAccessKey="True">
<ContentPresenter.Resources>
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Ellipse.Fill" Value="Gold"/>
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
</Border>
</ControlTemplate>
</TabItem.Template>
<TabItem.Header>
<Ellipse Stroke="Black" StrokeThickness="2" Width="100" Height="30" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</TabItem.Header>
</TabItem>
Move your style one level upper.ie,move it to ControlTemplate.Resources and it will work fine.I am quite not sure why the code in the question does not work.It may be because the controls in the contentpresenter is already built by the time the style is encountered.
<ControlTemplate x:Name="theTabItemControlTemplate" TargetType="{x:Type TabItem}">
<ControlTemplate.Resources>
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Fill" Value="Red"/>
</Style>
</ControlTemplate.Resources>
<Border BorderBrush="DarkBlue" BorderThickness="10">
<Grid>
<ContentPresenter ContentSource="Header" RecognizesAccessKey="True">
</ContentPresenter>
</Grid>
</Border>
</ControlTemplate>

WPF custom progress bar clipping

I've created a custom progress bar as follows:
<!-- Custom progress bar -->
<Style
x:Key="CopyProgressBar"
TargetType="ProgressBar">
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="ProgressBar">
<Grid>
<Border
x:Name="PART_Track"
CornerRadius="5"
BorderBrush="#BBC6C4"
BorderThickness="2" />
<Rectangle
x:Name="PART_Indicator"
Fill="#A5B2B0"
RadiusX="5"
RadiusY="5"
Margin="3"
HorizontalAlignment="Left" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Here's how it is used:
<ProgressBar
x:Name="copyProgress"
Height="13"
Width="279"
Canvas.Left="158"
Canvas.Top="103"
Minimum="0"
Maximum="100"
Style="{StaticResource CopyProgressBar}" />
It works quite well, but when the progress is full, the right side of the fill bar is clipped, which removes the rounding styling I'm going for. I've fiddled with padding, margins, max width, etc, but I can't find a way to prevent the clipping.
Here is an image:
This was an interesting one to answer. I finally got it nailed down to being the margin that was causing the problem. The progress bar sets the width of PART_Indicator based on the width of PART_Track regardless of the margin or paddings that are set. The following style will get you the desired behaviour.
<Style x:Key="CopyProgressBar" TargetType="ProgressBar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ProgressBar">
<Border BorderBrush="#BBC6C4" BorderThickness="1" CornerRadius="5" Padding="1">
<Grid x:Name="PART_Track" >
<Rectangle x:Name="PART_Indicator" HorizontalAlignment="Left" Fill="#A5B2B0" RadiusX="5" RadiusY="5"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Does anyone have a simple example of a UserControl with a single ContentPresenter?

So far, I have this:
<UserControl
x:Class="MyConcept.ExpanderPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Border
Style="{StaticResource Border_PanelStyle}"
CornerRadius="3" />
<ContentPresenter />
</Grid>
</UserControl>
Sample usage of this UserControl:
<nc:ExpanderPanel
Grid.Row="0">
<Expander
IsExpanded="True"
Header="NMT Users">
<StackPanel>
...
</StackPanel>
</Expander>
</nc:ExpanderPanel>
Discussion
If I run this, I see nothing. No content is presented, not even the border that is built into the UserControl.
I thought maybe I needed to make the ContentPresenter a dependency property, but I couldn't figure out how I would link the property to the ContentPresenter in the UserControl's XAML.
Can someone provide a simple example that shows how to build a UserControl (or some kind of custom control) with a single ContentPresenter?
ContentPresenters are main used in ControlTemplates and bound with a TemplateBinding to the ContentControl.Content.
from this site... a control template for a button that uses a ContentPresenter
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Rectangle Fill="{TemplateBinding Property=Background}" />
<ContentPresenter
Content="{TemplateBinding Property=ContentControl.Content}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Resources