これをプロパティにバインドするか、単にブラシ リソースを使用するかどうかはわかりません。リソースを使用した例を次に示します。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="ControlPressedColor">#FF211AA9</SolidColorBrush >
<ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type ToggleButton}">
<Grid>
<Ellipse x:Name="outerCircle" Width="Auto" Height="Auto" StrokeThickness="4" Fill="White" Stroke="Gray"/>
<Label x:Name="content" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Gray"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{StaticResource ControlPressedColor}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="outerCircle" Property="Fill" Value="LightGray"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{Binding Path=Foreground, RelativeSource='{RelativeSource TemplatedParent}'}"/>
<Setter TargetName="content" Property="Foreground" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<ToggleButton Template="{StaticResource MyButtonTemplate}" Height="50" Width="50"></ToggleButton>
</Grid>
</Window>
ToggleButton でプロパティを設定できるようにしたいという明確化に基づいて、ここでDependecy プロパティを使用する必要があります。簡単な例を次に示します。
カスタム コントロール
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
public class CustomControl2 : System.Windows.Controls.Primitives.ToggleButton
{
public static readonly DependencyProperty myFillColorProperty = DependencyProperty.Register("myFillColor",typeof(SolidColorBrush),typeof(System.Windows.Controls.Primitives.ToggleButton));
static CustomControl2()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl2), new FrameworkPropertyMetadata(typeof(CustomControl2)));
}
public SolidColorBrush myFillColor
{
get { return (SolidColorBrush)GetValue(myFillColorProperty); }
set { SetValue(myFillColorProperty, value); }
}
}
}
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my ="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type ToggleButton}">
<Grid>
<Ellipse x:Name="outerCircle" Width="Auto" Height="Auto" StrokeThickness="4" Fill="White" Stroke="Gray"/>
<Label x:Name="content" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Gray"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{Binding Path=myFillColor, RelativeSource='{RelativeSource TemplatedParent}'}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="outerCircle" Property="Fill" Value="LightGray"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{Binding Path=Foreground, RelativeSource='{RelativeSource TemplatedParent}'}"/>
<Setter TargetName="content" Property="Foreground" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<my:CustomControl2 myFillColor="Red" Template="{StaticResource MyButtonTemplate}" Height="50" Width="50"></my:CustomControl2>
</Grid>
</Window>