CustomControl在清理后可见,但在重建后不可见

时间:2015-09-25 08:10:19

标签: c# wpf xaml user-controls

几个星期前我和WPF一起开始,所以我是新手。 我试图创建一个UserControl。

但它在窗口中不可见。 如果我清理项目,则UserControl可见并且带有带文本的警告标志:

"此文档包含一个或多个已更改的控件。重建项目以在设计视图中显示更改。"

当我重建或运行(包括重建)时,UserControl消失。

这就是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>

这个是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);

    }
}

}

这是我在MainWindow中实现UserControl的方式:

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

我试图将XAML放入MainWindow,看看是否失败但是没有失败,我试图创建一个空的自定义用户控件,它产生与ColorPicker相同的结果。

1 个答案:

答案 0 :(得分:0)

好的,问题就出现了问题:您已将ctor静态设置为注册所有DependencyProperties,因此您不再拥有必须调用ctor的常用课程InitializeComponents() 。因此,请在您的课程中添加ctor

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

要了解此次通话的原因,请访问this page