Привет! В общем, создал свой User Control по примеру. Выглядит он следующим образом. Файл .cs: Код | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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 Vofka.Controls { /// <summary> /// Логика взаимодействия для ColorPicker.xaml /// </summary> public partial class ColorPicker : UserControl { public ColorPicker() { InitializeComponent(); }
public static DependencyProperty ColorProperty;
public static DependencyProperty RedProperty; public static DependencyProperty GreenProperty; public static DependencyProperty BlueProperty;
static ColorPicker() { // Регистрация свойств зависимости ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorChanged))); RedProperty = DependencyProperty.Register("Red", typeof(byte), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorRGBChanged))); GreenProperty = DependencyProperty.Register("Green", typeof(byte), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorRGBChanged)));
BlueProperty = DependencyProperty.Register("Blue", typeof(byte), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorRGBChanged))); }
public Color Color { get { return (Color)GetValue(ColorProperty); } set { SetValue(ColorProperty, value); } } public byte Red { get { return (byte)GetValue(RedProperty); } set { SetValue(RedProperty, value); } } public byte Green { get { return (byte)GetValue(GreenProperty); } set { SetValue(GreenProperty, value); } } public byte Blue { get { return (byte)GetValue(BlueProperty); } set { SetValue(BlueProperty, value); } }
private static void OnColorRGBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { ColorPicker colorPicker = (ColorPicker)sender; Color color = colorPicker.Color; if (e.Property == RedProperty) color.R = (byte)e.NewValue; else if (e.Property == GreenProperty) color.G = (byte)e.NewValue; else if (e.Property == BlueProperty) color.B = (byte)e.NewValue;
colorPicker.Color = color; //colorPicker.Color = Colors.Red; }
private static void OnColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { Color newColor = (Color)e.NewValue; ColorPicker colorPicker = (ColorPicker)sender;
colorPicker.Red = newColor.R; colorPicker.Green = newColor.G; colorPicker.Blue = newColor.B;
}
} }
|
Файл .xaml такой: Код | <UserControl x:Class="Vofka.Controls.ColorPicker" 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:wrfApp2="clr-namespace:Vofka.Controls" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <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="sliderRed" Minimum="0" Maximum="255" Margin="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type wrfApp2:ColorPicker}},Path=Padding, Mode=TwoWay}" Value="{Binding Path=Red, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type wrfApp2:ColorPicker}}, Mode=TwoWay}" ></Slider> <Slider Name="sliderGreen" Minimum="0" Maximum="255" Grid.Row="1" Margin="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type wrfApp2:ColorPicker}},Path=Padding, Mode=TwoWay}" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type wrfApp2:ColorPicker}}, Path=Green, Mode=TwoWay}"></Slider> <Slider Name="sliderBlue" Minimum="0" Maximum="255" Grid.Row="2" Margin="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type wrfApp2:ColorPicker}},Path=Padding, Mode=TwoWay}" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type wrfApp2:ColorPicker}}, Path=Blue, Mode=TwoWay}"></Slider> <Rectangle Grid.Column="1" Grid.RowSpan="3" Width="75" Stroke="Black" StrokeThickness="1" StrokeDashArray="2 1"> <Rectangle.Fill> <SolidColorBrush Color="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type wrfApp2:ColorPicker}},Path=Color, Mode=TwoWay}"></SolidColorBrush> </Rectangle.Fill> </Rectangle> </Grid> </UserControl>
|
Создаю новое окно, в файле .xaml вставляю этот контрол: Код | <local:ColorPicker x:Name="colorPicker"></local:ColorPicker>
|
Контрол отображается на форме:  В итоге, когда передвигаю слайдеры - то ничего не происходит. В отладчике смотрю, в свойстве public Color Color - цвет изменяется, т.е. переменная содержит действительное значение цвета, с учетом того, что я слайдер подергал. Но в форме квадратик цвет не меняет. Так же прошу обратить внимание на закоментированную строку colorPicker.Color = Colors.Red; в методе OnColorRGBChanged. Если её раскоментировать - то при передвижении любого слайдера, квадратик меняет цвет на красный и слайдер, который отвечает за красный цвет (верхний) передвигается до упора справа. Подскажите, что за беда.
|