failed to create menuitem click method in resource directory - wpf

I'm struggling with this below problem. my style file in resource directory. but it can't apply click method to context menu item. it's showing below this error. please help me how can i achieve this.
Error : "'Failed to create a 'Click' from the text 'OnMenuItemClick'.' Line number '10' and line position '35'."
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:src="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ObjectDataProvider x:Key="date" ObjectType="{x:Type src:DateTime}"/>
<Style x:Key="ContextMenuStyle1" TargetType="{x:Type ContextMenu}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
<ContextMenu x:Key="ListViewContext" Style="{StaticResource ContextMenuStyle1}">
<MenuItem Header="Create" Click="OnMenuItemClick" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"></MenuItem>
</ContextMenu>
<ContextMenu x:Key="GridItemContext" Style="{StaticResource ContextMenuStyle1}">
<MenuItem Header="Modify" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
<MenuItem Header="Delete" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
</ContextMenu>
<Style x:Key="ListViewGrid" TargetType="{x:Type ListView}">
<Setter Property="BorderBrush" Value="#FFDFDFE2"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Background" Value="#faf2f2"/>
<Setter Property="ContextMenu" Value="{StaticResource ListViewContext}"/>
</Style>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="ContextMenu" Value="{StaticResource GridItemContext}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border CornerRadius="0" SnapsToDevicePixels="True" >
<Border Name="InnerBorder" CornerRadius="0" BorderThickness="0,0,0,1" BorderBrush="#FFDFDFE2">
<Grid Background="#FFEFEFEF" Name="Trg" Height="20">
<GridViewRowPresenter />
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Trg" Property="Background" Value="#FFDFDFE2" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Trg" Property="Background" Value="#FFDFDFE2" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

When I tried the above code, the error I got was:
Error 1 'ResourceDictionary' root element requires a x:Class attribute
to support event handlers in the XAML file. Either remove the event
handler for the Click event, or add a x:Class attribute to the root
element. Line 10 Position 35
This is how I fixed it:
1) Added x:Class attribute to the ResourceDictionary:
x:Class="WpfApplication4.Dictionary1"
2) Added a C# class file to the project, Code below:
public partial class Dictionary1 : ResourceDictionary
{
public Dictionary1()
{
InitializeComponent();
}
void OnMenuItemClick(object sender, RoutedEventArgs e)
{
}
}
And then, the project built fine.

Related

WPF- How to refactor multiple ListViewItem Style Template with few changes?

I'm using custom style selector on list view to change CornerRadius / BorderThickness for first and last item.
I need to have this presentaion with Enum binding on listview
Code of the list view with ItemContainerStyleSelector
<ListView ItemsSource="{Binding ItemsSource, ElementName=Self, Mode=TwoWay}"
FocusVisualStyle="{x:Null}"
BorderThickness="0"
SelectedValue="{Binding SelectedValue, ElementName=Self, Mode=TwoWay}"
HorizontalAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Background="Transparent">
<ListView.ItemContainerStyleSelector>
<enumToggleButtonList:FirstLastItemStyleSelector
DefaultStyle="{StaticResource AllItemStyle}"
StyleFirst="{StaticResource FirstItemStyle}"
StyleLast="{StaticResource LastItemStyle}"/>
</ListView.ItemContainerStyleSelector>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid FlowDirection="LeftToRight" Rows="1"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
I have 3 styles : default / first / last like this
<Style x:Key="AllItemStyle" TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border x:Name="TemplateBorder"
Style="{StaticResource BorderListViewItemStyle}"
CornerRadius="0"
BorderThickness="1,1,0,1">
<TextBlock x:Name="TemplateTextBlock"
Text="{Binding}"
Style="{StaticResource TextblockListViewItemStyle}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="TemplateBorder" Property="Style" Value="{StaticResource SelectedBorderListViewItemStyle}"/>
<Setter TargetName="TemplateTextBlock" Property="Style" Value="{StaticResource SelectedTextblockListViewItemStyle}"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="TemplateBorder" Property="Style" Value="{StaticResource BorderListViewItemStyle}"/>
<Setter TargetName="TemplateTextBlock" Property="Style" Value="{StaticResource TextblockListViewItemStyle}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
</Style>
<Style x:Key="LastItemStyle" TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border x:Name="TemplateBorder"
Style="{StaticResource BorderListViewItemStyle}"
CornerRadius="0,5,5,0">
<TextBlock x:Name="TemplateTextBlock"
Text="{Binding}"
Style="{StaticResource TextblockListViewItemStyle}"/>
</Border>
.... same
</Style>
<Style x:Key="FirstItemStyle" TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border x:Name="TemplateBorder"
Style="{StaticResource BorderListViewItemStyle}"
CornerRadius="5,0,0,5"
BorderThickness="1,1,0,1">
<TextBlock x:Name="TemplateTextBlock"
Text="{Binding}"
Style="{StaticResource TextblockListViewItemStyle}"/>
</Border>
.... same
</Style>
And globally changes are for the border :
<Border x:Name="TemplateBorder"
Style="{StaticResource BorderListViewItemStyle}"
CornerRadius="5,0,0,5"
BorderThickness="1,1,0,1">
There are any methods to refactor this without using code behind plz ? Or another way to do that ?
You can simplify your styles by utilizing attached properties. But that does require you creating a simple attached properties class.
using System.Windows;
namespace SO
{
public static class ListViewItemProperties
{
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.RegisterAttached(
"CornerRadius",
typeof(Thickness),
typeof(ListViewItemProperties),
new PropertyMetadata(new Thickness(0d)));
public static Thickness GetCornerRadius(DependencyObject obj)
{
return (Thickness)obj.GetValue(CornerRadiusProperty);
}
public static void SetCornerRadius(DependencyObject obj, Thickness value)
{
obj.SetValue(CornerRadiusProperty, value);
}
}
}
And there are the updated (and simplified) styles that use the attached property:
<Style x:Key="AllItemStyle" TargetType="ListViewItem">
<Setter Property="local:ListViewItemProperties.CornerRadius" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border x:Name="TemplateBorder"
BorderThickness="1,1,0,1"
CornerRadius="{TemplateBinding local:ListViewItemProperties.CornerRadius}"
Style="{StaticResource BorderListViewItemStyle}">
<TextBlock x:Name="TemplateTextBlock"
Style="{StaticResource TextblockListViewItemStyle}"
Text="{Binding}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="TemplateBorder" Property="Style" Value="{StaticResource SelectedBorderListViewItemStyle}" />
<Setter TargetName="TemplateTextBlock" Property="Style" Value="{StaticResource SelectedTextblockListViewItemStyle}" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="TemplateBorder" Property="Style" Value="{StaticResource BorderListViewItemStyle}" />
<Setter TargetName="TemplateTextBlock" Property="Style" Value="{StaticResource TextblockListViewItemStyle}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<EventSetter
Event="PreviewMouseLeftButtonDown"
Handler="ListViewItem_PreviewMouseLeftButtonDown" />
</Style>
<Style x:Key="LastItemStyle" BasedOn="{StaticResource AllItemStyle}" TargetType="ListViewItem">
<Setter Property="local:ListViewItemProperties.CornerRadius" Value="0,5,5,0" />
</Style>
<Style x:Key="FirstItemStyle" BasedOn="{StaticResource AllItemStyle}" TargetType="ListViewItem">
<Setter Property="local:ListViewItemProperties.CornerRadius" Value="5,0,0,5" />
</Style>
The most simplest /sexy solution to do that is for the moment TemplateBinding ... I just discovered how it work ... learning is hard .. but the way after is easier x)
<Style x:Key="AllItemStyle" TargetType="ListViewItem">
<Setter Property="Border.CornerRadius" Value="0"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border x:Name="TemplateBorder"
Style="{StaticResource BorderListViewItemStyle}"
CornerRadius="{TemplateBinding Border.CornerRadius}"
BorderThickness="{TemplateBinding BorderThickness}">
<TextBlock x:Name="TemplateTextBlock"
Text="{Binding}"
Style="{StaticResource TextblockListViewItemStyle}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="TemplateBorder" Property="Style" Value="{StaticResource SelectedBorderListViewItemStyle}"/>
<Setter TargetName="TemplateTextBlock" Property="Style" Value="{StaticResource SelectedTextblockListViewItemStyle}"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="TemplateBorder" Property="Style" Value="{StaticResource BorderListViewItemStyle}"/>
<Setter TargetName="TemplateTextBlock" Property="Style" Value="{StaticResource TextblockListViewItemStyle}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
</Style>
<Style x:Key="LastItemStyle" TargetType="ListViewItem" BasedOn="{StaticResource AllItemStyle}">
<Setter Property="Border.CornerRadius" Value="0,10,10,0"/>
</Style>
<Style x:Key="FirstItemStyle" TargetType="ListViewItem" BasedOn="{StaticResource AllItemStyle}">
<Setter Property="Border.CornerRadius" Value="10,0,0,10"/>
<Setter Property="BorderThickness" Value="1,1,0,1"/>

Simple working example to override default visual style of a button in wpf

I want to create custom visual style for my buttons.
I need a simple working example of how to override default visual style of a button. As well as a simple explanation of how to apply it.
I want to get something working, so I can start from there and experiment my way further.
I've tried to add a new recourse dictionary as follows:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="mstyle" TargetType="Button">
<Setter Property="FontWeight" Value="Bold" />
</Style>
</ResourceDictionary>
after that I've created some new button in runtime and tried to apply this style to it:
Dim MyButton As New Button
Dim st As New Style
st = Application.Current.FindResource("mstyle")
MyButton.Style = st
When I try to run this, I get an error that the recourse 'mstyle' could not be found.
You don't in most cases need any code behind to do that all what you need is to define a custom style that target your button in the resource dictionary or in the window resource here an example :
<Style x:Key="DarkStyleButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#373737" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="4" Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#E59400" />
<Setter Property="Foreground" Value="White" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="White" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Gray" />
<Setter Property="Foreground" Value="LightGray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
first set the value for the properties you want to customize,
then set the button template and don't forget to add the
ContentPresenter that will hold the button content
finally define triggers to handle the mouse over, click and what ever
else you want to set a custom look when it triggers (for example the
desable/enabled )
To use that style here how
<Button x:Name="BrowseButton" Margin="5" Style="{StaticResource DarkStyleButton}" ToolTip="tooltip about the button">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image Source="../BrowseImage.png"/>
<TextBlock Text="Browse" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5"></TextBlock>
</StackPanel>
</Button.Content>
</Button>

Disable focus on tabstop when TextBox is readonly

When a TextBox is in readonly and on tabbing i want to disable the focus border of the textbox, i wanted to do this in Style of textbox.Can any one help me to achieve this?
Updated the content:
Screenshot:
Place a condition like the below in your textbox style
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsReadOnly" Value="True"></Condition>
<Condition Property="IsFocused" Value="True"></Condition> </MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="BorderBrush" Value="{#7B2F81}"></Setter>
</MultiTrigger.Setters>
Try this:
<Window.Resources>
<Style x:Key="FocusVisualStyle" TargetType="{x:Type Control}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<Border SnapsToDevicePixels="True"
CornerRadius="0"
BorderThickness="2"
BorderBrush="#7B2F81" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Pink" />
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisualStyle}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<TextBox Text="TestText"
IsReadOnly="True"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
If TextBox.IsReadOnly == True then set FocusVisualStyle in StyleTrigger. Visual behavior of Focus you can customize in FocusVisualStyle.

WPF Tab Styling

I am trying to style my WPF TabControl. I basically want to get the tab control to have a transparent background, with a white border and text. I want the selected tab to have a White Background and Transparent Text (or any colour text!). Essentially a 2 colour tab.
However, I cannot override the selected tab appearance - this shows as white. And my child textboxes are taking the style of the TabItem font. Note, in the screen shot my labels have their own style set so do not take the TabItem font.
I have the following XAML in place to do this. Ideally I want to create the styles so that I can reuse across the application.
Resource Dictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<Style x:Key="Tabs" TargetType="TabControl">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="White"/>
</Style>
<Style x:Key="TabItemStyle" TargetType="TabItem">
<Setter Property="Foreground" Value="White" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TabItem}}" Value="True">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="White"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TabItem}}" Value="False">
<Setter Property="Foreground" Value="LightGray"/>
<Setter Property="Background" Value="Transparent"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
Then the XAML MarkUp
<TabControl Style="{StaticResource Tabs}">
<TabItem Header="General" Style="{StaticResource TabItemStyle}">...</TabItem>
<TabItem Header="Details" Style="{StaticResource TabItemStyle}">...</TabItem>
<TabItem Header="Info" Style="{StaticResource TabItemStyle}">...</TabItem>
<TabItem Header="More Stuff..." Style="{StaticResource TabItemStyle}">...</TabItem>
</TabControl>
How can I style my tabs to be and prevent the children from sizing?
Your DataTriggers don't work. To fix it change RelatveSource to Self
Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}"
However I would suggest to change them to Triggers like so:
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="White"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Foreground" Value="LightGray"/>
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
You should create control template for TabItem.
This sample change TabItem background to Transparent and Text color to White.
You can use own color schema.
<Window.Resources>
<Style TargetType="TabControl">
<Setter Property="Background"
Value="Transparent" />
<Setter Property="BorderBrush"
Value="White" />
</Style>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border"
Margin="0,0,-4,0"
Background="{x:Static Brushes.White}"
BorderBrush="{x:Static Brushes.White}"
BorderThickness="1,1,1,1"
CornerRadius="2,12,0,0">
<ContentPresenter x:Name="ContentSite"
Margin="12,2,12,2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ContentSource="Header"
RecognizesAccessKey="True" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Panel.ZIndex"
Value="100" />
<Setter TargetName="Border"
Property="Background"
Value="{x:Static Brushes.Transparent}" />
<Setter TargetName="Border"
Property="BorderThickness"
Value="1,1,1,0" />
<Setter Property="TextBlock.Foreground"
Value="White" />
<!--<Setter Property="TextBlock.Foreground"
Value="Transparent" />-->
</Trigger>
<Trigger Property="IsEnabled"
Value="False">
<Setter TargetName="Border"
Property="Background"
Value="{x:Static Brushes.White}" />
<Setter TargetName="Border"
Property="BorderBrush"
Value="{x:Static Brushes.White}" />
<Setter Property="Foreground"
Value="{x:Static Brushes.White}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Background="SkyBlue">
<TabControl Margin="20">
<TabItem Header="TabItem #1">
<TextBox>Tab Item #1 content</TextBox>
</TabItem>
<TabItem Header="TabItem #2">
<TextBox>Tab Item #1 content</TextBox>
</TabItem>
<TabItem Header="TabItem #3">
<TextBox>Tab Item #1 content</TextBox>
</TabItem>
</TabControl>
</Grid>

Generic Button Template

I have the following button style that has a cross in it and is used for a close button on a TabItem
<Button Grid.Column="2"
Width="15"
Height="15"
HorizontalAlignment="Center"
VerticalAlignment="Center"
DockPanel.Dock="Right"
AttachedCommand:CommandBehavior.Event="Click"
AttachedCommand:CommandBehavior.Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CloseWorkspaceCommand}">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Background="{TemplateBinding Background}">
<Path x:Name="ButtonPath"
Margin="3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M0,0 L1,1 M0,1 L1,0"
SnapsToDevicePixels="True"
Stretch="Uniform"
Stroke="{DynamicResource CloseButtonStroke}"
StrokeEndLineCap="Flat"
StrokeStartLineCap="Flat"
StrokeThickness="2"/>
</Grid>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem}}"
Value="false" />
<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem}}"
Value="false" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Visibility" Value="Hidden" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Hidden" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background"
Value="{DynamicResource CloseButtonBackgroundHighlighted}" />
<Setter TargetName="ButtonPath"
Property="Stroke"
Value="{DynamicResource CloseButtonStrokeHighlighted}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background"
Value="{DynamicResource CloseButtonBackgroundPressed}" />
<Setter TargetName="ButtonPath"
Property="Stroke"
Value="{DynamicResource CloseButtonStroke}" />
<Setter TargetName="ButtonPath"
Property="Margin"
Value="2.5,2.5,1.5,1.5" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
This produces
I want to add a CheckButton styled to show a pin next to the close button like VS2012. I can do this no problem, but I want to place the CheckBox inside the same button template above so I get highlighting when the mouse is over the area. The problem is the button above contains the Path that draws the 'X'.
Is there a was I can make the button style above generic so I can include either the 'X' Path or the CheckBox, without replicating all the trigger stuff?
Thanks for your time.
I would approach this by creating my button class like ToolBarButton below and have a dependency property ButtonType. I will set ButtonType on the ToolBarButton.
public enum ButtonType
{
Close =0,
Pin
}
public class ToolBarButton : Button
{
public ButtonType ButtonType
{
get { return (ButtonType)this.GetValue(ButtonTypeProperty); }
set { this.SetValue(ButtonTypeProperty, value); }
}
public static readonly DependencyProperty ButtonTypeProperty = DependencyProperty.Register(
"ButtonType", typeof(ButtonType), typeof(ToolBarButton), new PropertyMetadata(ButtonType.Close));
}
in xaml:
<local:ToolBarButton ButtonType="Pin"/>
Then I will add the trigger in style to check for buttontype and apply the controltemplate containing checkbox
<Style.Triggers>
<Trigger Property="ButtonType" Value="Pin">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<CheckBox/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>

Resources