The template that I'm trying to create is displaying unwanted lines on some items. I cannot figure out why.
This is the control template:
<ControlTemplate TargetType="RadioButton" x:Key="SidebarItem">
<ControlTemplate.Resources>
<ResourceDictionary>
<Style TargetType="Border">
<Setter Property="Margin" Value="0"></Setter>
<Setter Property="Padding" Value="0"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="BorderBrush" Value="#312B57"></Setter>
</Style>
</ResourceDictionary>
</ControlTemplate.Resources>
<Border Background="#312b57">
<StackPanel Orientation="Vertical" Margin="25 0 0 0">
<Border Height="20" x:Name="TopContainer" >
<Border x:Name="Top" Background="#312B57" Padding="0"></Border>
</Border>
<Border x:Name="Middle" CornerRadius="10 0 0 10" Padding="15 8" Width="150" HorizontalAlignment="Right">
<TextBlock x:Name="TextBlock" Text="{TemplateBinding Content}"></TextBlock>
</Border>
<Border Height="20" x:Name="BottomContainer" Padding="0">
<Border x:Name="Bottom" Background="#312B57" Padding="0" ></Border>
</Border>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="Middle" Property="Background" Value="#fff"></Setter>
<Setter TargetName="TopContainer" Property="Background" Value="#fff"></Setter>
<Setter TargetName="BottomContainer" Property="Background" Value="#fff"></Setter>
<Setter TargetName="Top" Property="CornerRadius" Value=" 0 0 25 0"></Setter>
<Setter TargetName="TextBlock" Property="Foreground" Value="#312B57"></Setter>
<Setter TargetName="Bottom" Property="CornerRadius" Value="0 25 0 0"></Setter>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="TextBlock" Property="Foreground" Value="#fff"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
The implementation:
<ItemsControl x:Name="Sidebar" Background="Transparent" BorderThickness="0" Margin="0 25" BorderBrush="Transparent" Padding="0 10 0 0" ItemsSource="{x:Static viewModels:SidebarContent.Items}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type models:SidebarItem}">
<RadioButton GroupName="Sidebar" Template="{StaticResource SidebarItem}" IsChecked="{Binding IsSelected}" Content="{Binding DisplayName}"></RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And the issue:
The Correct:
Can someone please tell me why I am getting the extra line on some of the list items?
Those lines are rendering artifacts, you can get rid off the effect with SnapToDevicePixels:
You can set this property to true on your root element to enable pixel snap rendering throughout the UI. For devices operating at greater than 96 dots per inch (dpi), pixel snap rendering can minimize anti-aliasing visual artifacts in the vicinity of single-unit solid lines.
Set this property to true on the parent Border element in your control template, it will apply to all child elements, since it is inherited.
[...] only the outermost element in a subtree needs to specify SnapsToDevicePixels as true, and all child elements of that subtree will then report SnapsToDevicePixels as true and will have the SnapsToDevicePixels visual effect.
<Border Background="#312b57" SnapsToDevicePixels="True">
<StackPanel Orientation="Vertical" Margin="25 0 0 0">
<Border Height="20" x:Name="TopContainer" >
<Border x:Name="Top" Background="#312B57" Padding="0"></Border>
</Border>
<Border x:Name="Middle" CornerRadius="10 0 0 10" Padding="15 8" Width="150" HorizontalAlignment="Right">
<TextBlock x:Name="TextBlock" Text="{TemplateBinding Content}"></TextBlock>
</Border>
<Border Height="20" x:Name="BottomContainer" Padding="0">
<Border x:Name="Bottom" Background="#312B57" Padding="0" ></Border>
</Border>
</StackPanel>
</Border>
Alternatively, you could set the UseLayoutRounding property to true (which is recommended over SnapToDevicePixels), but in this case it still leaves a line between elements.
Related
I have a listbox with a template, inside the listboxitem I have 2 textblock.
My goal is to change the foreground of those 2 textblock when the listboxitem is:
Hovered(Mouse over)
Selected
My ItemTemplate currently removes the highlight when u hover over items, that works as expected.
I know I can get the IsSelected and IsMouseOver events, for example:
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="White"/>
<!-- I have no idea how to access the textblock child of the listboxitem to change it -->
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<!-- I have no idea how to access the textblock child of the listboxitem to change it -->
</Trigger>
But I could not figure out how to modify the TextBlock inside, I've tried using acestor and a whole different ways none worked(as in they had no effect).
Here is what my View looks like:
<UserControl x:Class="Shex.Views.SideBarView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Shex.Views"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:shex="clr-namespace:Shex"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="260"
Background="#403f42" Padding="0" BorderThickness="0" Margin="0">
<UserControl.Resources>
<Style x:Key="MenuContainerStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="0" Background="{TemplateBinding Background}"
Padding="0" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid Margin="0">
<ListBox ItemsSource="{Binding MenuItems}" SelectionMode="Single" SelectedItem="{Binding SelectedMenuItem}" SelectionChanged="{s:Action ChangeView}"
BorderThickness="0" Padding="0" Margin="0 10 0 0" Background="#403f42" ItemContainerStyle="{DynamicResource MenuContainerStyle}" SnapsToDevicePixels="true">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="60" Orientation="Horizontal" SnapsToDevicePixels="true">
<TextBlock Text="{Binding Image}" FontFamily="/Shex;component/Fonts/Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free Solid" Foreground="#cfced1" FontSize="20" Margin="10 6 0 0" Width="30" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Name}" Foreground="#cfced1" FontSize="20" Margin="10 6 0 0" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
You should add the trigger(s) to the ItemTemplate, e.g.:
<DataTemplate>
<StackPanel ...>
<TextBlock x:Name="tb1" ... />
<TextBlock x:Name="tb2" ... />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="True">
<Setter TargetName="tb1" Property="Foreground" Value="Blue" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
A ListBoxItem style cannot set properties of some individual TextBlock or any other control that is defined in the ItemTemplate.
I am trying to build a text filter for a list in a popup, and notify the user when the filter is active by changing the color of the button that opens the popup.
I am using an MVVM lite setup and a XAML style sheet to reference the desired styles. Up until this point I have created the popup and the controlling button, and I have been able to put a mouse over trigger on the button and have it work. However when I tried to have a datatrigger set to the value of the textbox in the popup, it does not respond at all.
Here is the XAML code in the View for the button and the popup:
<StackPanel Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Center" >
<TextBlock HorizontalAlignment="Center" Text="Filter Popup" />
<Button x:Name="PopUpControl" Tag="popup" Style="{StaticResource FilterButtonStyle}" Command="{Binding OpenPopupCommand}" IsEnabled="{Binding IsFilterEnabled, UpdateSourceTrigger=PropertyChanged}" Margin="2,1,2,1" VerticalAlignment="Top"/>
</StackPanel>
<Popup x:Name="textPopup" IsOpen="{Binding IsFilterPopupOpen, Mode=TwoWay}" PlacementTarget="{Binding ElementName=PopUpControl}" Placement="Bottom" Width="Auto" StaysOpen="False" Margin="2 2 2 2">
<TextBox x:Name="TextValue" Grid.Column="0" BorderThickness="1" Style="{StaticResource WatermarkedTextBox}" Margin="2,4" VerticalAlignment="Center" Tag="Text Filter" Text="{Binding FilterSearch, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
<Button Grid.Column="4" Style="{StaticResource SearchIconStyle}" IsEnabled="{Binding FilterEnabled, Mode=TwoWay}" Command="{Binding ApplyFilterCommand}" Margin="19,6" Grid.ColumnSpan="2" />
</Grid>
</Popup>
Here is the style for the button:
<Style x:Key="SortButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Width" Value="15"/>
<Setter Property="Height" Value="15"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Margin" Value="4,0,4,0" />
<Setter Property="Tag" Value="Default" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
Padding="4,2"
CornerRadius="3"
Background="{DynamicResource PaleBlue}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
<Border.OpacityMask>
<VisualBrush Visual="{StaticResource appbar_filter}" />
</Border.OpacityMask>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="Background" Value="{DynamicResource ColumnHeaderFilterColor}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
What I would like to happen, is when the element "TextValue" has a value in the Text field, the primary background color for the "border" changes color. Any help would be greatly appreciated.
You could use a DataTrigger that binds to the FilterSearch source property and sets a property if returns an empty string or null:
<ControlTemplate TargetType="Button">
<Border Name="border"
Padding="4,2"
CornerRadius="3"
Background="Green">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
<Border.OpacityMask>
<VisualBrush Visual="{StaticResource appbar_filter}" />
</Border.OpacityMask>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding FilterSearch}" Value="">
<Setter TargetName="border" Property="Background" Value="{DynamicResource PaleBlue}" />
</DataTrigger>
<DataTrigger Binding="{Binding FilterSearch}" Value="{x:null}">
<Setter TargetName="border" Property="Background" Value="{DynamicResource PaleBlue}" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
I am trying to set up a sort of menu using ListBox and buttons as ListBoxItems to trigger a command when selection changes, but the result is that some times the selected ListBoxItem is changed but the command is not triggered, other times the opposite. The XAML is the following:
<Window.Resources>
<DataTemplate DataType="{x:Type viewModels:TripViewModel}">
<views:TripView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:AddTripViewModel}">
<views:AddTripView />
</DataTemplate>
<Style TargetType="{x:Type Button}">
<Setter Property="Height"
Value="50" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border>
<Grid Background="Transparent">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Padding"
Value="3" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border BorderBrush="Transparent"
BorderThickness="0"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="DarkCyan" />
</Trigger>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="DarkCyan" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Window.Content>
<DockPanel Margin="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Height="Auto"
Width="Auto"
LastChildFill="True">
<!-- Left Panel -->
<Border BorderBrush="Black"
BorderThickness="0,0,1,0">
<StackPanel DockPanel.Dock="Left"
Margin="0"
Background="#555D6F">
<!-- First Row: Stacco Image -->
<Image Height="183"
Width="275"
Margin="3"
Source="/Media/stacco.png" />
<!-- Second Row: Buttons -->
<ScrollViewer CanContentScroll="True"
VerticalAlignment="Stretch"
VerticalScrollBarVisibility="Auto"
Margin="0,10,0,0">
<ListBox Name="MenuButtons"
ItemsSource="{Binding PageViewModels}"
Background="#555D6F"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<ItemContainerTemplate>
<Button Content="{Binding Name}"
Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
Margin="0" />
</ItemContainerTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</StackPanel>
</Border>
<!-- Control Area: Different View made by controls -->
<ContentControl DockPanel.Dock="Right"
Content="{Binding CurrentPageViewModel}" />
</DockPanel>
</Window.Content>
Am I doing right trying to insert a button in the ItemContainerTemplate? Would it be better to use the code behind to trigger the selection changed?
I am trying to implement style for TabControl along with TabItem like below Images:
The Style should make below things visible:
List item
White Background for TabControl and selected TabItem with Dropshadow Effect.
When any TabItem is not selected, then the TabItem text color should turn to gray.
What I have achieved till now:
Able to divide width of TabControl to accommodate TabItem items with equal Sizes using TabSizeConverter converter.
Able to change background and with of TabControl and TabItems. But not able to achieve Dropshadow effect.
Below is my Style for TabItem:
<Setter Property="Padding" Value="0"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Foreground" Value="{StaticResource color_MediumGray}"/>
<Setter Property="FontSize" Value="34"/>
<Setter Property="FontFamily" Value="Resources/Fonts/#HelveticaNeueMed" />
<Setter Property="Width">
<Setter.Value>
<MultiBinding Converter="{StaticResource tabSizeConverter}">
`<Binding RelativeSource="{RelativeSource Mode=FindAncestor,` AncestorType={x:Type TabControl}}" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}" Path="ActualWidth" />
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border x:Name="Chrome"
BorderThickness="30,0"
BorderBrush="{StaticResource color_Transparent}"
Background="{StaticResource color_LightGray}"
Padding="0" Margin="0">
<ContentPresenter ContentSource="Header"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter TargetName="Chrome" Property="BorderThickness" Value="0"/>
<Setter TargetName="Chrome" Property="Background" Value="{StaticResource color_White}"/>
<Setter Property="Foreground" Value="{StaticResource color_Purple}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If anyone can help me acheving TabControl with such style would be a great help.
Thanks in advance.
The Style should make below things visible:
List Item
White Background for TabControl and selected TabItem with Dropshadow Effect.
When any TabItem is not selected, then the TabItem text color should turn to gray.
I suppose this is just a typo?
Set TabControl.Background to white, set the drop shadow effect on the TabControl, set TabControl.BorderThickness to zero, set TabItem.Background and TabItem.BorderBrush to white, do not change TabItem.BorderThickness. For the tab header alignment, an easy fix for the TabPanel.Margin is usage of negative margin for selected tabs.
Set TextBlock.Foreground on Chrome instead of playing with TabItem.Foreground in the template triggers.
Generally note that I replaced your color constants with system color names for my testing. Also I didn't bother to re-solve the tab item width issue and instead assigned them a static width. Oh, and I used default fonts instead of your font resource :)
My complete sample:
<Window.Resources>
<Style x:Key="itemStyle" TargetType="TabItem">
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="FontSize" Value="34"/>
<Setter Property="FontFamily" Value="Resources/Fonts/#HelveticaNeueMed" />
<Setter Property="Width" Value="310"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border x:Name="Chrome"
BorderThickness="10,0"
BorderBrush="Transparent"
Background="LightGray"
TextBlock.Foreground="Gray"
Padding="0" Margin="0">
<ContentPresenter ContentSource="Header"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter TargetName="Chrome" Property="BorderBrush" Value="White"/>
<Setter TargetName="Chrome" Property="Background" Value="White"/>
<Setter TargetName="Chrome" Property="Margin" Value="-2,0,-2,-1"/>
<Setter TargetName="Chrome" Property="TextBlock.Foreground" Value="Purple"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid x:Name="grid1">
<Grid MaxWidth="650" MaxHeight="600">
<Border Background="Gray">
<Border.Effect>
<BlurEffect/>
</Border.Effect>
</Border>
<TabControl BorderThickness="0" Margin="5" Background="White">
<TabControl.Effect>
<DropShadowEffect />
</TabControl.Effect>
<TabItem Header="Postpaid" Style="{StaticResource itemStyle}" HorizontalContentAlignment="Center">
<WrapPanel>
<Rectangle Fill="Blue" Width="180" Height="180" Margin="10"/>
<Rectangle Fill="Green" Width="380" Height="180" Margin="10"/>
<Rectangle Fill="Blue" Width="180" Height="180" Margin="10"/>
<Rectangle Fill="Green" Width="180" Height="180" Margin="10"/>
<Rectangle Fill="Yellow" Width="180" Height="180" Margin="10"/>
</WrapPanel>
</TabItem>
<TabItem Header="Prepaid" Style="{StaticResource itemStyle}" HorizontalContentAlignment="Center">
<WrapPanel>
<Rectangle Fill="Green" Width="180" Height="180" Margin="10"/>
<Rectangle Fill="Yellow" Width="180" Height="180" Margin="10"/>
<Rectangle Fill="Blue" Width="180" Height="180" Margin="10"/>
<Rectangle Fill="Green" Width="380" Height="180" Margin="10"/>
<Rectangle Fill="Blue" Width="180" Height="180" Margin="10"/>
</WrapPanel>
</TabItem>
</TabControl>
</Grid>
</Grid>
In the following XAML I'm using a Rectangle with a Border as the Template for a ToggleButton.
I want the BorderBrush to be a different colour to reflect the changing value of ToggleButton.IsChecked.
Unfortunately my attempt here of using a TemplateBinding in the DataTrigger doesn't work. What do I need to do instead?
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<ControlTemplate x:Key="ButtonAsSwatchTemplate">
<Border BorderThickness="1">
<Border.Style>
<Style>
<Setter Property="BorderBrush" Value="Gainsboro" />
<Style.Triggers>
<!-- TemplateBinding doesn't work.-->
<DataTrigger
Binding={TemplateBinding Property=IsChecked}
Value="True">
<Setter Property="BorderBrush" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Rectangle Fill="{TemplateBinding Property=Background}"
Width="15" Height="15" />
</Border>
</ControlTemplate>
</StackPanel.Resources>
<ToggleButton Template="{StaticResource ButtonAsSwatchTemplate}"
HorizontalAlignment="Center" VerticalAlignment="Center"
Margin="2" Background="Red" />
</StackPanel>
EDIT
When I build and reload the designer I get the following error:
Error 1 Property 'Binding' does not support values of type 'TemplateBindingExpression'.
SOLUTION
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<ControlTemplate x:Key="ButtonAsSwatchTemplate">
<Border x:Name="SwatchBorder" BorderThickness="1">
<Rectangle Fill="{TemplateBinding Property=Background}" Width="15" Height="15" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="SwatchBorder" Property="BorderBrush" Value="Yellow" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</StackPanel.Resources>
<RadioButton Template="{StaticResource ButtonAsSwatchTemplate}"
GroupName="CropGuidesColourRadioButtonGroup"
IsChecked="{Binding Checked}" Margin="2" Background="Red" />
<RadioButton Template="{StaticResource ButtonAsSwatchTemplate}"
GroupName="CropGuidesColourRadioButtonGroup"
IsChecked="{Binding Checked}" Margin="2" Background="Black" />
...
</StackPanel>
You could use a Trigger in the ControlTemplate, e.g.
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<ControlTemplate x:Key="ButtonAsSwatchTemplate">
<Border x:Name="myBorder" BorderBrush="Gainsboro" BorderThickness="1">
<Rectangle Fill="{TemplateBinding Property=Background}" Width="15" Height="15" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="myBorder" Property="BorderBrush" Value="Black" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</StackPanel.Resources>
<ToggleButton Template="{StaticResource ButtonAsSwatchTemplate}"
HorizontalAlignment="Center" VerticalAlignment="Center"
Margin="2" Background="Red" />
</StackPanel>
One problem I see right away is that you are setting BorderBrush as a local property value on Border. This will always override the value applied by the style. Try removing the explicit BorderBrush attribute and see if that works.
Border BorderBrush="Gainsboro" BorderThickness="1"
Dependency Property Value Inheritance