Here is code of class for custom control.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
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 Colorbox
{
public class ColorPickerControl :System.Windows.Controls.Control
{
static ColorPickerControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPickerControl),new FrameworkPropertyMetadata(typeof(ColorPickerControl)));
colorproperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPickerControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(Oncolorchanged)));
redproperty = DependencyProperty.Register("Red", typeof(byte), typeof(ColorPickerControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnRGBcolorchanged)));
greenproperty = DependencyProperty.Register("Green", typeof(byte), typeof(ColorPickerControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnRGBcolorchanged)));
blueproperty = DependencyProperty.Register("Blue", typeof(byte), typeof(ColorPickerControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnRGBcolorchanged)));
CommandManager.RegisterClassCommandBinding(typeof(ColorPickerControl), new CommandBinding(ApplicationCommands.Stop, stopexecute));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
RangeBase slider = GetTemplateChild("PART_RedSlider") as RangeBase;
if (slider != null)
{
Binding binding = new Binding("Red");
binding.Source = this;
binding.Mode = BindingMode.TwoWay;
slider.SetBinding(RangeBase.ValueProperty, binding);
}
slider = GetTemplateChild("PART_GreenSlider") as RangeBase;
if (slider != null)
{
Binding binding = new Binding("Green");
binding.Source = this;
binding.Mode = BindingMode.TwoWay;
slider.SetBinding(RangeBase.ValueProperty, binding);
}
slider = GetTemplateChild("PART_BlueSlider") as RangeBase;
if (slider != null)
{
Binding binding = new Binding("Blue");
binding.Source = this;
binding.Mode = BindingMode.TwoWay;
slider.SetBinding(RangeBase.ValueProperty, binding);
}
SolidColorBrush brush = GetTemplateChild("PART_PreviewBrush") as SolidColorBrush;
if (brush != null)
{
Binding binding = new Binding("Color");
binding.Source = brush;
binding.Mode = BindingMode.OneWayToSource;
this.SetBinding(ColorPickerControl.colorproperty, binding);
}
}
}
}
Here is Generic.xaml file code
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Colorbox">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Colorbox;component/Themes/ColorPickerControl.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Generic file reference to my defaultstyle file.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Colorbox;assembly=Colorbox">
<Style TargetType="{x:Type local:ColorPickerControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ColorPickerControl}">
<Border removed="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Slider Name="PART_RedSlider" Minimum="0" Maximum="255"
Margin="{TemplateBinding Padding}"></Slider>
<Slider Grid.Row="1" Name="PART_GreenSlider" Minimum="0" Maximum="255"
Margin="{TemplateBinding Padding}"></Slider>
<Slider Grid.Row="2" Name="PART_BlueSlider" Minimum="0" Maximum="255"
Margin="{TemplateBinding Padding}"></Slider>
<Rectangle Grid.Column="1" Grid.RowSpan="3"
Margin="{TemplateBinding Padding}"
Width="50" Stroke="Black" StrokeThickness="1">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=Color,
RelativeSource={RelativeSource TemplatedParent}}"></SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Next i implement my custom control in Main window.
xmlns:lib="clr-namespace:Colorbox;assembly=Colorbox" <lib:ColorPickerControl Name="choosecolor2" Color="Red" Height="200" Width="200" Colorchanged="choosecolor_Colorchanged_1" />
In result i get empty custom control.I overriden default style key,created themes folder with generic.xaml,help me find the problem,or at least give me hint how to solve it.Custom control is situated in other project,reference was added.