this is the image
I need Button Control Template as shown in the image.So can anyone help me to do it?
This piece of code in your app.xaml
<Application.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Image Source="DHXMR.jpg" />
<ContentControl Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
will turn out to show buttons like that:
Of course you need to remove the white "Button" text from the sample image you provided in your link - but I guess you're able to do that on your own
Related
I'm trying to create a new style for a ListItem to get rid of the default blue rectangles showing when hovering the items and selecting them.
here's what I've got so far:
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<ContentPresenter/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Problem is - content is not showing at all. I tried to do it with a different controller (a button, for example). ContentPresenter doesn't seem to work with ListBoxItem though...
How do I achieve that?
You need to add TargetType="ListBoxItem" to ControlTemplate
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<StackPanel Orientation="Horizontal">
<ContentPresenter/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I have a ContentControl like this:
<ContentControl>
<userControls:TestControl/>
</ContentControl>
OR like this [when i have PRISM system]:
<ContentControl prism:RegionManager.RegionName="TestView"/>
I see the final UserControl well until this step when i start the program.
In the above samples the Content type is UserControl. Now i want give a ControlTemplate to this ContentControl. Then i created a style named StyleTest and used it in my Xaml:
<ContentControl Style="{StaticResource StyleTest}"> .....
My style:
<Style x:Key="StyleTest" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Margin="10">
<Border CornerRadius="10" BorderBrush="#ffffffff" BorderThickness="5">
<StackPanel>
<ContentPresenter Content="{Binding}"/>
<TextBlock>Some additional text to test template</TextBlock>
</StackPanel>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
But when i start the program the UserControl can not be seen and i just see this text and a border around it: Some additional text to test template
What i must write instead of above line of code to see my UserControl again with a white border around it?
Why the UserControl not showing with above code (above style)?
There are 3 ways to do this.
- by setting the ContentTemplate
- by setting the Template
- or using the Border directly and apply a style.
In this case I would use the Border and apply a style because it looks like the ContentControl is only used to do add a styled Border.
<StackPanel>
<StackPanel.Resources>
<Style x:Key="BorderStyle" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="5" />
<Setter Property="CornerRadius" Value="10" />
<Setter Property="Margin" Value="10" />
</Style>
<Style x:Key="ContentTemplateStyle" TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border Style="{StaticResource BorderStyle}">
<!-- Bind to the DataContext of the DataTemplate which is the Content of the ContentControl -->
<!-- <ContentPresenter Content="{Binding}" />-->
<!-- TemplateBinding improves performance -->
<ContentPresenter Content="{TemplateBinding Content}" />
<!-- Using the TemplatedParent -->
<!--<ContentPresenter Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>-->
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ControlTemplateStyle" TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Border Style="{StaticResource BorderStyle}">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources>
<ContentControl Style="{StaticResource ContentTemplateStyle}">
<Button>ContentTemplateStyle</Button>
</ContentControl>
<ContentControl Style="{StaticResource ControlTemplateStyle}">
<Button>ControlTemplateStyle</Button>
</ContentControl>
<Border Style="{StaticResource BorderStyle}">
<Button>BorderStyle</Button>
</Border>
</StackPanel>
datacontext is a type?. Normally it will be inherited in the visual tree so I think it should be left blank.
Content is the content of the parent. Normally you can use a template binding or set the content source property.
but using this template will only show you a white border around content. The original template is lost. So you should provide the entire template for the control.
Now maybe the control contains margin and border properties and you can set those from your style and leave the original template in place.
I found my mistake place and changed this part of my codes:
<Style x:Key="StyleTest" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
To this one:
<Style x:Key="StyleTest" TargetType="ContentControl">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate >
When i play with Template its effects is on inner content of the ContentControl (in this question: template of the UserControl) But when i play with ContentTemplate, its effects is on ContentControl layout.
ContentPresenter tag is same and it works now...
<ContentPresenter Content="{Binding}"/>
I am new to WPF and I created the following simple style example. But it doesn't work properly and button's content doesn't show although I can still click on it. Can anyone tell me why it is broken?
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Blue"
BorderThickness="5"
Background="Aqua"
Width="80"
Height="40">
<ContentPresenter></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="Grid" x:Name="GridWithMarginStyle">
<Setter Property="Margin" Value="12"></Setter>
</Style>
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<EventSetter Event="Button.Click" Handler="ButtonHandler" />
<Setter Property="Background" Value="Red"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
</Style>
</StackPanel.Resources>
<Button Name="OkBtn">OK</Button>
<Button Name="CancelBtn" Click="CancelBtn_Click">Cancel</Button>
</StackPanel>
You are using the BasedOn property in the correct way. The problem is that your ContentPresenter is not binded to the control it renders (i.e. the button).
Just try to replace your ControlTemplate XAML with this one:
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="Blue"
BorderThickness="5"
Background="Aqua"
Width="80"
Height="40">
<ContentPresenter Content="{TemplateBinding Content}" />
</Border>
</ControlTemplate>
By using TemplateBinding you can bind the ContentPresenter to the Content property of your templated control.
For example, i have the following Style:
<Style x:Key="MyStyle" TargetType="MyType">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MyType">
<Grid>
<Button x:Name="MyButton"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Is it possible to inherit from him a different style and change the properties of the button "MyButton"? For example change the property Visibility?
Thank You!
There are more than one way of achieving this.
Use BasedOn
<Style TargetType="MyChildType" BasedOn="{StaticResource MyStyle}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MyType">
<Grid>
<Button x:Name="MyButton" Visibility="Collapsed"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
Apply style to MyButton and put Trigger in Style.Triggers to change the properties of button depending on the properties of parent
I created a button. My basic requirements are rounded thicker border, with more than one color (i.e. for Buy/Sell buttons)
I was hoping that i could create the template once, and than just override the border brush like this:
<Style x:Key="BorderButton">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderThickness="2"
BorderBrush="Red"
CornerRadius="3"
Background="{x:Null}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="GreenBorderButton" BasedOn="{StaticResource BorderButton}" TargetType="{x:Type Button}">
<Setter Property="BorderBrush" Value="Green" />
</Style>
but they both produce the same style.
Do i need to write out the whole template every time? seems like unnecessary code repetition (especially if 3-4 colors are desired). Hoping there is some way to inherit a template.
Your code is very close to working; the issue is that GreenBorderButton is applying the BorderBrush to the button itself, not the Border in the overridden Template.
To fix this, simply change the Border's BorderBrush to use the parent Button's BorderBrush. You can do this using a TemplateBinding like so:
<Style x:Key="BorderButton">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderThickness="2"
BorderBrush="{TemplateBinding Property=BorderBrush}"
CornerRadius="3"
Background="{x:Null}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then, you can either use the same overridden styles like you have, or you could simply do:
<Button Style="{StaticResource BorderButton}" BorderBrush="Blue" Content="Blue" />