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.