WPF System.Windows.Media.Color A value must be set, display ball and number in canvas ,mouse down to refresh

FredGrit發表於2024-09-19
 private Color GetRndColor()
 {
     Color cr = new Color();
     cr.A = 255;
     cr.R = (byte)(rnd.Next(0, 255));
     cr.G = (byte)(rnd.Next(0, 255));
     cr.B = (byte)(rnd.Next(0, 255));
     return cr;
 }

//usercontrol.xaml
<UserControl x:Class="WpfApp382.EllipseTextBlockBrush"
             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:WpfApp382"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Ellipse Fill="{Binding FillColorBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Text="{Binding FillStr,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 WpfApp382
{
    /// <summary>
    /// Interaction logic for EllipseTextBlockBrush.xaml
    /// </summary>
    public partial class EllipseTextBlockBrush : UserControl
    {
        public EllipseTextBlockBrush()
        {
            InitializeComponent();
            this.DataContext = this;
        }



        public SolidColorBrush FillColorBrush
        {
            get { return (SolidColorBrush)GetValue(FillColorBrushProperty); }
            set { SetValue(FillColorBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FillColorBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FillColorBrushProperty =
            DependencyProperty.Register("FillColorBrush", typeof(SolidColorBrush),
                typeof(EllipseTextBlockBrush), new PropertyMetadata(null));




        public string FillStr
        {
            get { return (string)GetValue(FillStrProperty); }
            set { SetValue(FillStrProperty, value); }
        }

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



    }
}


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

namespace WpfApp382
{
    /// <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()
        {
            int width = (int)this.ActualWidth - 100;
            int height = (int)this.ActualHeight - 100;
            for(int i=0;i<100;i++)
            {
                EllipseTextBlockBrush elp = new EllipseTextBlockBrush();
                elp.Width = 50;
                elp.Height = 50;
                elp.FillColorBrush = new SolidColorBrush(GetRndColor());
                elp.FillStr = $"{i + 1}";
                Canvas.SetLeft(elp,rnd.Next(0,width));
                Canvas.SetTop(elp,rnd.Next(0,height));
                if(!cvs.Children.Contains(elp))
                {
                    cvs.Children.Add(elp);
                }
            }
        }

        private Color GetRndColor()
        {
            Color cr = new Color();
            cr.A = 255;
            cr.R = (byte)(rnd.Next(0, 255));
            cr.G = (byte)(rnd.Next(0, 255));
            cr.B = (byte)(rnd.Next(0, 255));
            return cr;
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            cvs.Children.Clear();
            DrawElps();
        }
    }
}

相關文章