wpf为自定义控件添加属性

wpf为自定义控件添加属性

首先,创建UserControl.xaml文件,命名为MyUserButton,之后改成这种样式

<Button x:Class="UserButtonTest.MyUserButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:UserButtonTest"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Background="Transparent" BorderThickness="0">

    <Grid>
        <Border Height="50" BorderThickness="3" CornerRadius ="5" Background="#FFFFCC" BorderBrush="#FF6633">
            <Border BorderThickness="0">
                <Viewbox VerticalAlignment="Center" HorizontalAlignment="Center">
                    <TextBlock Name="tb" Text="Default Text"></TextBlock>
                </Viewbox>
            </Border>
        </Border>
    </Grid>
</Button>

打开对应的.cs文件,做如下修改


public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(MyUserButton), new           PropertyMetadata("TextBox", new PropertyChangedCallback(OnTextChanged)));

public string Text
{
  get { return (string)GetValue(TextProperty); }
  set { SetValue(TextProperty, value); }
}

static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    ((MyUserButton)sender).OnValueChanged(args);
}
protected void OnValueChanged(DependencyPropertyChangedEventArgs e)
{
    this.tb.Text = e.NewValue.ToString();
}

然后在MainWindow.xaml中引用这个控件

<local:MyUserButton Text="ddd" Width="200" Height="100" ClickF="MyUserButton_ClickF"></local:MyUserButton>

运行,效果如下
这里写图片描述

但是这样做有问题:Button周围有一大片作用区域,是模板定义得有问题,但如果不这样做的话,MyUserButton.cs中无法找到对应的tb控件。试了好久也不得法。如果你把问题解决了,Please @me。
这里写图片描述

WPF自定义控件添加属性可以通过创建依赖属性或附加属性来完成。以下是这两种属性的基本概念和实现方式: 1. 依赖属性(Dependency Property):依赖属性WPF特有的属性系统,它允许属性值能够响应各种动态变化,比如样式和模板改变。在控件添加依赖属性通常需要使用`DependencyProperty.Register`方法,并且需要定义一个依赖属性的后台存储。依赖属性的值可以由不同的来源提供,包括默认值、数据绑定、样式、主题模板等。 2. 附加属性(Attached Property):附加属性是一种特殊的依赖属性,它可以让其他控件拥有或改变属性的值。附加属性通常用于将属性值从一个控件应用到另一个控件上。定义附加属性通常使用`DependencyProperty.RegisterAttached`方法。 下面是一个简单的示例,演示如何在WPF自定义控件添加依赖属性: ```csharp public class MyCustomControl : Control { static MyCustomControl() { // 注册依赖属性 CustomProperty = DependencyProperty.Register( "Custom", // 属性名 typeof(string), // 属性类型 typeof(MyCustomControl), // 所属的类型 new FrameworkPropertyMetadata(string.Empty) // 元数据设置 ); } // 依赖属性的定义 public static readonly DependencyProperty CustomProperty; // 依赖属性的公共访问器 public string Custom { get { return (string)GetValue(CustomProperty); } set { SetValue(CustomProperty, value); } } // ... 控件的其他定义 ... } ``` 通过上述代码,我们定义了一个名为`Custom`的依赖属性,它属于`MyCustomControl`这个自定义控件。开发者可以在XAML中使用这个属性,或者在代码中通过`GetValue`和`SetValue`方法来获取和设置它的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值