//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