wpf附加属性学习

本文介绍了一种在WPF中创建自定义控件的方法,通过实例演示了如何使用XAML和C#代码实现带有附加属性的按钮控件,包括绑定属性、样式设置和事件触发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

demo的xaml代码

<Window x:Class="WpfApp2.Login"
        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:WpfApp2"
        mc:Ignorable="d"
        Title="Login" Height="300" Width="500" BorderBrush="#FFABADB3" Background="#FFFFE7E7">
    <Window.Resources>
        <Style x:Key="myBtn" TargetType="{x:Type local:AttachProperty}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:AttachProperty}" >
                        <Grid>
                            <Ellipse Fill="{TemplateBinding Background}"/>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{TemplateBinding Text1}"  FontSize="{TemplateBinding Text1FontSize}"/>
                                <TextBlock Text="{TemplateBinding Text2}"  FontSize="{TemplateBinding FontSize}"/>
                            </StackPanel>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Foreground" Value="Red"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="button"/>
    </Window.Triggers>
    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup"/>
        </VisualStateManager.VisualStateGroups>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBox Grid.Row="0" Height="30" Width="300" Margin="20" Background="#FFFFE7E7">
        </TextBox>
        <PasswordBox Grid.Row="1" Height="30"  Width="300" Margin="20" Background="#FFFFE7E7" BorderBrush="#FFABADB3"></PasswordBox>
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <local:AttachProperty x:Name="bt1" Width="120" Grid.Column="0" Text1FontSize="30" Text2="测试" Text1="取消" FontSize="20" Style="{StaticResource myBtn}" Background="#FFEEB7B7" Content="取消" HorizontalAlignment="Left" Margin="73,17,0,13"/>
            <Button x:Name="button" Height="20" Width="100" Grid.Column="1" Command="" Background="#FFEEB7B7" BorderBrush="#FF707070">确定
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="Red"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </Grid>
        <TextBlock HorizontalAlignment="Left" Margin="26,38,0,0" TextWrapping="Wrap" Text="账号" VerticalAlignment="Top"/>
        <TextBlock HorizontalAlignment="Left" Margin="26,38,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF0E0E0E"><Run Text="密码"/><InlineUIContainer>
            </InlineUIContainer></TextBlock>
        <DatePicker HorizontalAlignment="Left" Margin="198,33,0,0" Grid.Row="2" VerticalAlignment="Top"/>
    </Grid>
</Window>

自定义控件附加属性的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp2
{
    public class AttachProperty:Button
    {
        public static readonly DependencyProperty Text1Property = DependencyProperty.RegisterAttached("Text1", typeof(string), typeof(AttachProperty));

        public string Text1
        {
            get { return (string)GetValue(Text1Property); }
            set
            {
                SetValue(Text1Property, value);
            }
        }
        public static readonly DependencyProperty Text2Property = DependencyProperty.RegisterAttached("Text2", typeof(string), typeof(AttachProperty));

        public string Text2
        {
            get { return (string)GetValue(Text2Property); }
            set
            {
                SetValue(Text2Property, value);
            }
        }
        public static readonly DependencyProperty Text1FontSizeProperty = DependencyProperty.RegisterAttached("Text1FontSize", typeof(double), typeof(AttachProperty));

        public double Text1FontSize
        {
            get { return (double)GetValue(Text1FontSizeProperty); }
            set
            {
                SetValue(Text1FontSizeProperty, value);
            }
        }
    }
}

初次接触记录一下,对于自定义控件里的属性,想要通过templatebinding绑定的话,在附加属性代码里类型要一致,否则会无效。这里重写了一个button,有两个textblock,可通过外部改一些属性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值