0

I started a few weeks ago with WPF so im new to it. I tried to create a UserControl.

But it is not visible in the Window. If I clean the Project the UserControl is visible and has a Warning Sign with the Text:

"This document contains one or more controls which have been changed. Rebuild the project to show the changes in the design view."

When I rebuild or Run (includes a rebuild) the UserControl Disappears.

Thats the XAML

    <Slider Name="sliderRed" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Minimum="0" Maximum="255" Value="{Binding ElementName=colorPicker, Path=Red}"/>
    <Slider Name="sliderGreen" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Minimum="0" Maximum="255" Value="{Binding ElementName=colorPicker, Path=Green}"/>
    <Slider Name="sliderBlue" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Top" Minimum="0" Maximum="255" Value="{Binding ElementName=colorPicker, Path=Blue}"/>

    <Rectangle Grid.Column="1" Grid.RowSpan="3">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding ElementName=colorPicker,Path=Color}"/>
        </Rectangle.Fill>
    </Rectangle>

And this one is the Class behind the XAML:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CustomControls
{
public partial class ColorPicker : UserControl
{
    public static DependencyProperty ColorProperty;

    public static DependencyProperty RedProperty;
    public static DependencyProperty GreenProperty;
    public static DependencyProperty BlueProperty;

    public static readonly RoutedEvent ColorChangedEvent;

    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); }
    }

    public event RoutedPropertyChangedEventHandler<Color> ColorChanged
    {
        add { AddHandler(ColorChangedEvent, value); }
        remove { RemoveHandler(ColorChangedEvent, value); }
    }

    static ColorPicker()
    {
        ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPicker), new FrameworkPropertyMetadata(Colors.Black, 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)));

        ColorChangedEvent = EventManager.RegisterRoutedEvent("ColorChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<Color>), typeof(ColorPicker));
    }

    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;
    }

    private static void OnColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        Color newColor = (Color)e.NewValue;
        Color oldColor = (Color)e.OldValue;

        ColorPicker colorPicker = (ColorPicker)sender;
        colorPicker.Red = newColor.R;
        colorPicker.Green = newColor.G;
        colorPicker.Blue = newColor.B;

        RoutedPropertyChangedEventArgs<Color> args = new RoutedPropertyChangedEventArgs<Color>(oldColor, newColor);
        args.RoutedEvent = ColorPicker.ColorChangedEvent;

        colorPicker.RaiseEvent(args);

    }
}

}

This is the way I implement the UserControl in the MainWindow:

<Window x:Class="CustomControls.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CustomControls"
    x:Name="mainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Background="ForestGreen">
<Grid>
    <local:ColorPicker Grid.Column="0" Grid.Row="0" Height="300" Width="300"/>
</Grid>

I have Tried to put the XAML into the MainWindow to see if that fails but that didn't fail and i tried to create an empty Custom User Control which produced the same results like the ColorPicker.

if-trubite
  • 159
  • 9
  • Your snippets are not completed: where are declared your `ColorProperty`, `RedProperty`, etc ? – tomab Sep 25 '15 at 08:44
  • They are just not shown because I Thought this would be clear I will show the whole Class. it hasn't space in a Comment. – if-trubite Sep 25 '15 at 08:59
  • You think so but there might be some issues. The biggest question mark is your static `ctor`; so is better to include them as well. – tomab Sep 25 '15 at 09:01

1 Answers1

0

Ok, so here is the problem: you made your ctor static, to register all DependencyProperties so you no longer have the usual class ctor which must call InitializeComponents(). So add in your class this ctor:

public ColorPicker()
{
    this.InitializeComponent();
}

To know why this call should occur, visit this page

Community
  • 1
  • 1
tomab
  • 2,061
  • 5
  • 27
  • 38
  • Good, now the issue is the Dep Properties which do not work. – tomab Sep 25 '15 at 09:18
  • The Control showed the expected Behaviour for me. I think that one with the Dependency Properties was not as clear as I thought to me. I would like to try this one by myself and ask later if I don't achieve to solve it alone :) – if-trubite Sep 25 '15 at 09:30
  • Ok, so it means there are some parts of code which are different between your sample and mine. Also you should mark the answer as correct and if there are any other thinks you might want to ask, they should be put in a separate questions. – tomab Sep 25 '15 at 09:34
  • hm that sould not be the case.. I did :) Yep that's what I Thought to myself! But I can not Upvote your answer because my Reputation is to low ^^ – if-trubite Sep 25 '15 at 09:40