Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is code of class for custom control.
C#
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
{
    /// <summary>
    /// Логика взаимодействия для ColorPickerControl.xaml
    /// </summary>
    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
XML
<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.
XML
   <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.
XML
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.
Posted
Updated 28-Aug-15 5:33am
v3
Comments
Richard Deeming 28-Aug-15 9:46am    
What settings do you have for the assembly:ThemeInfo attribute in the Colorbox assembly?
Inimicos 28-Aug-15 9:59am    
[assembly: ThemeInfo(
// Specifies the location of system theme-specific resource dictionaries for this project.
// The default setting in this project is "None" since this default project does not
// include these user-defined theme files:
// Themes\Aero.NormalColor.xaml
// Themes\Classic.xaml
// Themes\Luna.Homestead.xaml
// Themes\Luna.Metallic.xaml
// Themes\Luna.NormalColor.xaml
// Themes\Royale.NormalColor.xaml
ResourceDictionaryLocation.SourceAssembly,

// Specifies the location of the system non-theme specific resource dictionary:
// Themes\generic.xaml
ResourceDictionaryLocation.SourceAssembly)]
Richard Deeming 28-Aug-15 10:03am    
That looks OK. Try adding a key to your style:
<Style x:Key="{x:Type local:ColorPickerControl}" TargetType="{x:Type local:ColorPickerControl}">
Inimicos 28-Aug-15 10:08am    
No result((

1 solution

When i created one more project i should have use windows application,but i created class lybrary.In class library is impossible to create custom control,but user control works fine.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900