首页 > 其他分享 >WPF Customize control

WPF Customize control

时间:2024-08-15 16:05:46浏览次数:14  
标签:control EllipseTbk DependencyProperty Customize System Windows using WPF public

//xaml
<UserControl x:Class="WpfApp246.EllipseTbk"
             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:WpfApp246"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Ellipse Fill="{Binding ElpColor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Text="{Binding TbStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                   VerticalAlignment="Center" HorizontalAlignment="Center"
                   FontSize="30"/>
    </Grid>
</UserControl>


//xaml.cs
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 WpfApp246
{
    /// <summary>
    /// Interaction logic for EllipseTbk.xaml
    /// </summary>
    public partial class EllipseTbk : UserControl
    {
        public EllipseTbk()
        {
            InitializeComponent();
            this.DataContext = this;
        }
         
        public Brush ElpColor
        {
            get { return (Brush)GetValue(ElpColorProperty); }
            set { SetValue(ElpColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ElpColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElpColorProperty =
            DependencyProperty.Register("ElpColor", typeof(Brush), 
                typeof(EllipseTbk), new PropertyMetadata(Brushes.Red));




        public int TbStr
        {
            get { return (int)GetValue(TbStrProperty); }
            set { SetValue(TbStrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TbStr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TbStrProperty =
            DependencyProperty.Register("TbStr", typeof(int),
                typeof(EllipseTbk), new PropertyMetadata(0));

    }
}

 

 

//whole
//xaml
<UserControl x:Class="WpfApp246.EllipseTbk"
             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:WpfApp246"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Ellipse Fill="{Binding ElpColor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Text="{Binding TbStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                   VerticalAlignment="Center" HorizontalAlignment="Center"
                   FontSize="30"/>
    </Grid>
</UserControl>


//xaml.cs
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 WpfApp246
{
    /// <summary>
    /// Interaction logic for EllipseTbk.xaml
    /// </summary>
    public partial class EllipseTbk : UserControl
    {
        public EllipseTbk()
        {
            InitializeComponent();
            this.DataContext = this;
        }
         
        public Brush ElpColor
        {
            get { return (Brush)GetValue(ElpColorProperty); }
            set { SetValue(ElpColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ElpColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElpColorProperty =
            DependencyProperty.Register("ElpColor", typeof(Brush), 
                typeof(EllipseTbk), new PropertyMetadata(Brushes.Red));




        public int TbStr
        {
            get { return (int)GetValue(TbStrProperty); }
            set { SetValue(TbStrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TbStr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TbStrProperty =
            DependencyProperty.Register("TbStr", typeof(int),
                typeof(EllipseTbk), new PropertyMetadata(0));

    }
}



//cs
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 WpfApp246
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Random rnd { get; set; }
        private double width { get; set; }
        private double height { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            this.WindowState = WindowState.Maximized;
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            rnd = new Random();
            width = this.ActualWidth;
            height = this.ActualHeight; 
            DrawEllipseTextBlock();
        }

        private void DrawEllipseTextBlock()
        {
            Canvas cvs = new Canvas();
            for (int i = 0; i < 10; i++)
            {
                EllipseTbk elp = new EllipseTbk();
                elp.Width = 100;
                elp.Height = 100;
                switch (i%3)
                {
                    case 0:
                        elp.ElpColor = Brushes.Red;
                        break;
                    case 1:
                        elp.ElpColor = Brushes.Green;
                        break;
                    case 2:
                        elp.ElpColor = Brushes.Blue;
                        break;
                }
                elp.TbStr = i + 1;
                Canvas.SetLeft(elp, rnd.Next(0, (int)width)-100);
                Canvas.SetTop(elp, rnd.Next(0, (int)height)-100);
                if (!cvs.Children.Contains(elp))
                {
                    cvs.Children.Add(elp);
                }
                this.Content = cvs;
            }
        }

       }
}

 

标签:control,EllipseTbk,DependencyProperty,Customize,System,Windows,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18361165

相关文章

  • 阿里大佬写的 Controller 太优雅了!
    优雅的controller@RestController@RequestMapping("/user/test")public class UserController1 {    private static Logger logger = LoggerFactory.getLogger(UserController1.class);    @Autowired    private UserService userService;    @Autow......
  • [nRF24L01+] 3. Radio Control 无线电控制
    3.RadioControl无线电控制nRF24L01+可以配置为:powerdown,standby,Rx/Txmode3.1.无线控制状态图当VDD电压大于1.9V时,进入上电复位状态,并保持复位状态,直到进入掉电模式:需要注意的是:从待机模式到TX/RX模式的过度时间,和反过来,从TX/RX模式到待机模式的过渡时间是相同的;......
  • SMHC Three SD/MMC host controller (SMHC) interfaces
    SMHCThreeSD/MMChostcontroller(SMHC)interfaces1TheSMHC0controlsthedevicesthatcomplywiththeprotocolSecureDigitalMemory(SDmem-version3.0)2TheSMHC1controlsthedevicethatcomplieswiththeprotocolSecureDigitalI/O(SDIO-version......
  • WPF-实现多语言的静态(需重启)与动态切换(不用重启)
    目录一、多语言切换(需重启)1、配置文件添加Key2、新增附加属性当前选择语言3、创建资源文件 4、初始化多语言集合5、切换多语言并更新配置文件6、应用程序启动根据配置切换多语言 7、使用二、多语言切换(无需重启)1、创建多语言标记扩展基类2、添加资源转换器3、创......
  • CAD二次开发入门:WPF类库
     参考学习视频:https://www.bilibili.com/video/BV16Y411v7kr/?spm_id_from=333.337.search-card.all.click&vd_source=fbb64ea20b269b753497bf6c2499fc29 第一步:创建WPF类库,并写CAD调用方法  修改输出类型为:类库 添加CAD开发需要的类库 main页面添加以下内容:......
  • wpf ValidationRule 校验数据输入
    publicclassCountValidationRule:ValidationRule{publicoverrideValidationResultValidate(objectvalue,System.Globalization.CultureInfocultureInfo){doubled=0.0;if(double.TryParse((string)value,out......
  • 【WPF】【XAML】Exception: 无法找到名为“xx”的资源。资源名称区分大小写。
    <Grid.Resources>一定要放在使用之前,比如<ListBoxName="peopleListBox"Grid.Column="1"Grid.Row="2"ItemsSource="{BindingSource={StaticResourceExpenseDataSource},XPath=Person}"ItemTemplate="{StaticResour......
  • 摘要生成—通过摘要风格控制摘要的生成/抽取,原文阅读与理解:GEMINI: Controlling The S
    GEMINI:ControllingTheSentence-LevelSummaryStyleinAbstractiveTextSummarizationGEMINI:在抽象文本摘要中控制句子级摘要风格paper:https://arxiv.org/abs/2304.03548github:https://github.com/baoguangsheng/gemini本文介绍了一种自适应摘要抽取/生成方......
  • 控制SD图片生成的神经网络模型--ControlNet
    ControlNet是一个通过添加额外条件来控制SD中图像生成的神经网络,可以使用ControlNet来做以下事情:指定人体姿势。从另一幅图像复制图片的构图。生成参考图片类似的图像。将涂鸦图片变成专业的图像。ControlNet是用于控制SD的神经网络模型。您可以将ControlNet......
  • wpf 如何写一个圆形的进度条
    先看一下效果吧调用代码如下<local:CycleProgressBarWidth="100"Height="100"Background="#FFF68986"Foreground="#FFFA1F09"Maximum="100"Minimum="0"Value="20"IsIndeter......