WPF用户自定义控件

创建一个WPF自定义控件,同时为它添加依赖属性

1. 新建一个解决方案 WpfCustomControlTest

2. 新建一个用户自定义控件的类库 取名为WpfCustom1

  将customecontrol1.cs 改名为 TBcontrol.cs,同时修文件里面的类名

注意:原来的控件customecontrol1默认的关联样式在文件 Themes/Generic.xaml中,这时候也要修改这个文件让样式

和新控件名TBcontrol关联起来.

 关于 DefaultStyleKeyProperty.OverrideMetadata(typeof(TBControl), new FrameworkPropertyMetadata(typeof(TBControl)));是用来重写自定义控件的元数据,因为WPF不支持父类样式自动应用于

子类。如果屏掉这句话,可以添加一个显示的控件KEY来解决: 

   <Window.Resources>

        <Style TargetType="TextBox">

            <Setter Property="FontSize" Value="25" />

            <Setter Property="Background"  Value="Green" />

        </Style>

    </Window.Resources>

    <StackPanel>

        <TextBox   Text="aaa" />

        <local:CustomTextBox Text="bbb" Style="{StaticResource {x:Type TextBox}}" />

  </StackPanel> 

 

3. 为新控件注册一个依赖属性 (代码下载类TBControl中)如下

 

 public int NewValue

        {

            get

            {

                return (int)this.GetValue(NewValueProperty);

            }

 

            set

            {

                this.SetValue(NewValueProperty, value);

            }

        }

 

        public static readonly DependencyProperty NewValueProperty = DependencyProperty.Register(

          "NewValue",

          typeof(int),

          typeof(TBControl),

          new PropertyMetadata(0));

 

这样就能在这个控件的属性里面看到这个NewValue了。

4. 下面为这个控件添加设计,新建一个新类库 取名为WpfCustom1.Design

注意名称必须是控件类库名称+".Design",同时这个类库的build输出地址必须和Step2中的一样

 这个类库引用之前创建的WpfCustom1,同时还要引用以下的6个dll:PresentationCore,PresentationFramework,WindowsBase, System.Xaml,Microsoft.Windows.Design.PropertyEditing,Microsoft.Windows.Design.Interaction

  为刚创建的新属性NewValue添加可编辑的功能:

  4.1. 先添加一个样式

    添加文件EditorResources.cs 代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

 

namespace Controls.Design

{

    public partial class EditorResources : ResourceDictionary //注意是partial

    {

        public EditorResources()

            : base()

        {

            InitializeComponent();

        }

    }

}

     添加一个ResourceDictionary文件 命名: EditorResources .xaml 
内容如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:PropertyEditing="clr-namespace:Microsoft.Windows.Design.PropertyEditing;assembly=Microsoft.Windows.Design.Interaction"
                    xmlns:Local="clr-namespace:Controls.Design"
                    x:Class="Controls.Design.EditorResources">
    <DataTemplate x:Key="ComplexInlineEditorTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0" Text="{Binding StringValue}"/>
            <PropertyEditing:EditModeSwitchButton Grid.Column="1"/>
        </Grid>
        </DataTemplate>
</ResourceDictionary>
  
  4.3 删除Class1.cs文件 ,添加Metadata.cs文件 内容为
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfCustom1.Design;
using Microsoft.Windows.Design.Features;
using Microsoft.Windows.Design.Metadata;
using Microsoft.Windows.Design.PropertyEditing;
 
[assembly: ProvideMetadata(typeof(WpfCustom1.Design.Metadata))]
namespace WpfCustom1.Design
{
    internal class Metadata : IProvideAttributeTable
    {
        // Accessed by the designer to register any design-time metadata.
        public AttributeTable AttributeTable
        {
            get
            {
                AttributeTableBuilder builder = new AttributeTableBuilder();
 
                builder.AddCustomAttributes(
                    typeof(TBControl),
                    "NewValue",
                    PropertyValueEditor.CreateEditorAttribute(
                       typeof( PropertyValueEditor1   )));
 
                return builder.CreateTable();
            }
        }
    }
}
 
5. 创建PropertyValueEditor1.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Design.PropertyEditing;
using System.Windows;
 
namespace WpfCustom1.Design
{
    public class PropertyValueEditor1 : PropertyValueEditor
    {
         private EditorResources res = new EditorResources();
 
         public PropertyValueEditor1()
        {
            this.InlineEditorTemplate = res["NewValueInlineEditorTemplate"] as DataTemplate;   
            
        }
 
         public override void ShowDialog(
           PropertyValue propertyValue,
           IInputElement commandSource)
         {
          
         }
    }
}

转载于:https://www.cnblogs.com/starryliu/archive/2012/04/20/2459051.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值