首页 > 其他分享 >WPF Customcontrol with ellipse and textblock display randomly in canvas of mainwindow

WPF Customcontrol with ellipse and textblock display randomly in canvas of mainwindow

时间:2024-09-19 21:50:46浏览次数:13  
标签:canvas Windows Customcontrol System public elpTbk new using mainwindow

//usercontrol.xaml
<UserControl x:Class="WpfApp381.ElpImgTbk"
             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:WpfApp381"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Ellipse Fill="{Binding ElpFillBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                     />
        <TextBlock Text="{Binding ElpStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                   HorizontalAlignment="Center" 
                   VerticalAlignment="Center"
                   FontSize="30"
                   Foreground="Red"/>
    </Grid>
</UserControl>


//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 WpfApp381
{
    /// <summary>
    /// Interaction logic for ElpImgTbk.xaml
    /// </summary>
    public partial class ElpImgTbk : UserControl
    {
        public ElpImgTbk()
        {
            InitializeComponent();
            this.DataContext = this;
        }



        public ImageBrush ElpFillBrush
        {
            get { return (ImageBrush)GetValue(ElpFillBrushProperty); }
            set { SetValue(ElpFillBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ElpFillBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElpFillBrushProperty =
            DependencyProperty.Register("ElpFillBrush", typeof(ImageBrush), 
                typeof(ElpImgTbk), new PropertyMetadata(null));




        public string ElpStr
        {
            get { return (string)GetValue(ElpStrProperty); }
            set { SetValue(ElpStrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ElpStr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElpStrProperty =
            DependencyProperty.Register("ElpStr", typeof(string),
                typeof(ElpImgTbk), new PropertyMetadata(""));




    }
}


//mainwindow.xaml
<Window x:Class="WpfApp381.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:WpfApp381"
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Canvas x:Name="cvs"/>
</Window>


//mainwindow.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;
using System.IO;

namespace WpfApp381
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Random rnd { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            rnd = new Random();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DrawElps();
        }

        private void DrawElps()
        {
            var imgsList = Directory.GetFiles("../../Images");
            int width = (int)(this.ActualWidth) - 100;
            int height = (int)(this.ActualHeight) - 80;
            if (imgsList != null && imgsList.Any())
            {
                int imgsCount = imgsList.Count();
                for (int i = 0; i < 100; i++)
                {
                    ElpImgTbk elpTbk = new ElpImgTbk();
                    elpTbk.Width = 100;
                    elpTbk.Height = 60;
                    BitmapImage bmi = new BitmapImage();
                    bmi.BeginInit();
                    bmi.UriSource = new Uri(imgsList[i % imgsCount], UriKind.RelativeOrAbsolute);
                    bmi.EndInit();
                    bmi.Freeze();
                    elpTbk.ElpFillBrush = new ImageBrush(bmi);
                    elpTbk.ElpStr = $"{i+1}";
                    Canvas.SetLeft(elpTbk, rnd.Next(0, width));
                    Canvas.SetTop(elpTbk, rnd.Next(0, height));
                    if (!cvs.Children.Contains(elpTbk))
                    {
                        cvs.Children.Add(elpTbk);
                    }
                }
            }
        }
    }
}

 

标签:canvas,Windows,Customcontrol,System,public,elpTbk,new,using,mainwindow
From: https://www.cnblogs.com/Fred1987/p/18421450

相关文章

  • 诚邀见证2024九章云极DataCanvas算力包产品发布会!
    算力,是驱动全球智能化升级的关键力量,也是智算经济腾飞的主要燃料。在智算经济腾起之际,我们洞察未来:算力不仅是生产力还将作为社会性普惠AI资源。我们思考未来:算力产品和模式需要何种创新才能够加速算力普惠的进程?我们定义未来:用单位来标准化算力,用AI基础软件纳管算力!作为人工......
  • 【Canvas与诗词】《登科后》唐.孟郊
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>昔日龌龊不足夸</title><styletype="text/css"&......
  • Canvas简历编辑器-Monorepo+Rspack工程实践
    Canvas简历编辑器-Monorepo+Rspack工程实践在之前我们围绕Canvas聊了很多代码设计层面的东西,在这里我们聊一下工程实践。在之前的文中我也提到过,因为是本着学习的态度以及对技术的好奇心来做的,所以除了一些工具类的库例如 ArcoDesign、ResizeObserve、Jest 等包之外,关于数据结......
  • 【Canvas与钟表】儿童房表盘
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>孩子钟表</title><styletype="text/css">......
  • 【Canvas与表盘】绘制黄蓝双色简约表盘
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>黄蓝卡通手表</title><styletype="text/css"&g......
  • 【Canvas与电脑桌面】黑色方块黄十字背景桌面 1920x1080
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>黑色方块黄十字的密铺1920x1080</title><styletype="te......
  • 【Canvas与密铺】正六边形、正方形和正三角形的密铺
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>正六边形正方形和正三角形的密铺1920x1080</title><stylety......
  • 【Canvas与密铺】90年代马赛克密铺效果 1920x1080
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>20世纪90年代马赛克瓷砖效果1920x1080</title><styl......
  • 【Canvas与密铺】正六边形、正方形和正三角形的密铺
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>正六边形正方形和正三角形的密铺1920x1080</title><styletype=&quo......
  • 【Canvas与密铺】90年代马赛克密铺效果 1920x1080
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>20世纪90年代马赛克瓷砖效果1920x1080</title><styletype=&quo......