Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<CheckBox x:Class="WpfApp1.ImageCheckBox"
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:WpfApp1"
Name="RootCheckBox"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<CheckBox.Template>
<ControlTemplate TargetType="{x:Type CheckBox}">
<DockPanel>
<CheckBox x:Name="Foo" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}" />
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="Foo" Property="Background" Value="{Binding CustomColor, ElementName=RootCheckBox}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for ImageCheckBox.xaml
/// </summary>
public partial class ImageCheckBox
{
public ImageCheckBox()
{
InitializeComponent();
}
public SolidColorBrush CustomColor
{
get { return (SolidColorBrush)GetValue(CustomColorProperty); }
set { SetValue(CustomColorProperty, value); }
}
// Using a DependencyProperty as the backing store for CustomColor. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CustomColorProperty =
DependencyProperty.Register("CustomColor", typeof(SolidColorBrush), typeof(ImageCheckBox));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.