WPF Blend 自定义控件

本文介绍如何利用WPF Blend进行自定义控件的设计,包括步骤1:使用Blend进行界面设计,步骤2:为控件添加专属属性以增强功能,以及步骤3:在MainWindow中成功调用并应用自定义控件。
部署运行你感兴趣的模型镜像


在这里插入图片描述
在这里插入图片描述

#1.使用Blend 设计控件

<UserControl x:Class="WpfApp25.UserControl1"
             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:WpfApp25"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <local:StatusColorConverter x:Key="statusColorConverter"></local:StatusColorConverter>
    </UserControl.Resources>
    <Grid>
        <Ellipse HorizontalAlignment="Left" Height="100" Stroke="#FFF5F2F2" VerticalAlignment="Top" Width="100" StrokeThickness="5"
      Fill="{Binding StatusP, Converter={StaticResource statusColorConverter},UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UserControl1}}}">
        </Ellipse>
    </Grid>
</UserControl>

#2.给控件添加自定义属性

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
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 WpfApp25
{

    public enum Status { Normal, Good, Bad }

    public class StatusColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Status && value != null)
            {
                Status status = (Status)value;
                var color = Colors.Gray;
                switch (status)
                {
                    case Status.Good:
                        color = Colors.Green; break;
                    case Status.Bad:
                        color = Colors.Red; break;
                }
                return  new LinearGradientBrush(color,Colors.White,100.0);
            }

            return null;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    public partial class UserControl1 : UserControl
    {
        public Status StatusP
        {
            get { return (Status)GetValue(StatusPProperty); }
            set { SetValue(StatusPProperty, value); }
        }
        public static readonly DependencyProperty StatusPProperty =
            DependencyProperty.Register("StatusP", typeof(Status), typeof(UserControl1), new PropertyMetadata(Status.Normal));
        public UserControl1()
        {
            InitializeComponent();
            StatusP = Status.Bad;
        }
    }
}

#3.MainWindow添加调用

<Window x:Class="WpfApp25.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:WpfApp25"
 
        mc:Ignorable="d"
        Title="MainWindow" Height="522" Width="814">
    <Grid x:Name="xgrid" Background="#FFDCD3D3">
        <local:UserControl1 x:Name="xbtn"  Margin="110,190,577,129" ></local:UserControl1>

        <Button  Click="Button_Click" Content="Normal" HorizontalAlignment="Left" Height="73.443" Margin="341.846,150.596,0,0" VerticalAlignment="Top" Width="233.679"/>
        <Button  Click="Button_Click1" Content="Green" HorizontalAlignment="Left" Height="73" Margin="342,365,0,0" VerticalAlignment="Top" Width="234"/>
        <Button  Click="Button_Click2" Content="Red" HorizontalAlignment="Left" Height="73" Margin="342,255,0,0" VerticalAlignment="Top" Width="234"/>
    </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;
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 WpfApp25
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            xbtn.StatusP = Status.Normal;
        }

        private void Button_Click1(object sender, RoutedEventArgs e)
        {
            xbtn.StatusP = Status.Good;
        }

        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            xbtn.StatusP = Status.Bad;
        }
    }
}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值