我的自定义控件为空 [英] My Custom control is empty

查看:59
本文介绍了我的自定义控件为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是自定义控件的类代码。

Here is code of class for custom control.

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



这是Generic.xaml文件代码


Here is Generic.xaml file code

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



对我的defaultstyle文件的通用文件引用。


Generic file reference to my defaultstyle file.

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

xmlns:lib="clr-namespace:Colorbox;assembly=Colorbox" <lib:ColorPickerControl Name="choosecolor2" Color="Red"  Height="200" Width="200" Colorchanged="choosecolor_Colorchanged_1" />

结果我得到空的自定义控件。我覆盖默认值样式键,用generic.xaml创建主题文件夹,帮我找到问题,或至少给我提示如何解决它。自定义控件位于其他项目中,引用已添加。

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.

推荐答案

当我创建另一个项目时,我应该使用windows应用程序,但我创建了类lybrary。在类库中无法创建自定义控件,但用户控件工作正常。
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.


这篇关于我的自定义控件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆