代码之家  ›  专栏  ›  技术社区  ›  tklustig

如何将椭圆的所有属性放入WPF应用程序的通用数据类型中

  •  0
  • tklustig  · 技术社区  · 6 年前

    我实现了以下列表:

    private List<Ellipse> ellipsen = new List<Ellipse>();
    

    我的意图是放置省略号的所有属性,它们的实现方式如下:

    <Window x:Class="Halma.MainWindow"
            WindowStyle="ToolWindow"
            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:Halma"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            mc:Ignorable="d"
            Title="Halma" Height="900" Width="1200">
        <DockPanel LastChildFill="True">
            <DockPanel DockPanel.Dock="TOP" LastChildFill="True">
                <Button DockPanel.Dock="Left" FontSize="14" Margin="3" Width="100" Click="Button_Click_0">Beenden</Button>
                <TextBlock FontSize="20" VerticalAlignment="Center" Text="Koordinaten" x:Name="TextBlock_Status_1" Canvas.Left="2" Canvas.Top="473" Height="58" Width="592"/>
                <TextBlock FontSize="14" VerticalAlignment="Center" Text="Spielstein" x:Name="TextBlock_Status_2" Canvas.Left="2" Canvas.Top="473" Height="30" Width="592"/>
            </DockPanel>
            <Canvas>
                <i:Interaction.Behaviors>
                    <local:Brett/>
                </i:Interaction.Behaviors>
            </Canvas>
            <Canvas Name="Halma">
                <Canvas.Children>
                    <Ellipse x:Name="R0C0" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="385" Canvas.Top="80" />
                <Ellipse x:Name="R0C1" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="385" Canvas.Top="135" />
                <Ellipse x:Name="R0C2" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="385" Canvas.Top="195" />
                <Ellipse x:Name="R0C3" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="385" Canvas.Top="255" />
                <Ellipse x:Name="R0C4" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="385" Canvas.Top="305" />
                <Ellipse x:Name="R0C5" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="435" Canvas.Top="110" />
                <Ellipse x:Name="R0C6" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="435" Canvas.Top="165" />
                <Ellipse x:Name="R0C7" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="435" Canvas.Top="225" />
                <Ellipse x:Name="R0C8" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="435" Canvas.Top="280" />
                <Ellipse x:Name="R0C9" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="485" Canvas.Top="140" />
                <Ellipse x:Name="R0C10" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="485" Canvas.Top="200" />
                <Ellipse x:Name="R0C11" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="485" Canvas.Top="255" />
                <Ellipse x:Name="R0C12" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="535" Canvas.Top="170" />
                <Ellipse x:Name="R0C13" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="535" Canvas.Top="220" />
                <Ellipse x:Name="R0C14" Stroke="Black" StrokeThickness="2" Fill="Violet"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="585" Canvas.Top="195" />
    
                <Ellipse x:Name="L0C0" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="585" Canvas.Top="655" />
                <Ellipse x:Name="L0C1" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="635" Canvas.Top="625" />
                <Ellipse x:Name="L0C2" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="685" Canvas.Top="595" />
                <Ellipse x:Name="L0C3" Stroke="Brown" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="735" Canvas.Top="565" />
                <Ellipse x:Name="L0C14" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="735" Canvas.Top="630" />
                <Ellipse x:Name="L0C7" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="735" Canvas.Top="695" />
                <Ellipse x:Name="L0C4" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="785" Canvas.Top="535" />
                <Ellipse x:Name="L0C5" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="635" Canvas.Top="685" />
                <Ellipse x:Name="L0C6" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="685" Canvas.Top="655" />
                <Ellipse x:Name="L0C8" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="785" Canvas.Top="595" />
                <Ellipse x:Name="L0C9" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="685" Canvas.Top="715" />
                <Ellipse x:Name="L0c10" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="740" Canvas.Top="745" />
                <Ellipse x:Name="L0C11" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="785" Canvas.Top="770" />
                <Ellipse x:Name="L0C12" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="785" Canvas.Top="715" />
                <Ellipse x:Name="L0C13" Stroke="Black" StrokeThickness="2" Fill="Yellow"  MouseDown="Ellipse_MouseDown" MouseMove="rect_MouseMove" MouseLeftButtonDown="rect_MouseLeftButtonDown" MouseLeftButtonUp="rect_MouseLeftButtonUp" Height="30" Width="30" Canvas.Left="785" Canvas.Top="660" />
                </Canvas.Children>
            </Canvas>
        </DockPanel>
    </Window>

    上面列表中的构造函数。我尝试这样做,但失败了。我将得到错误,该画布不是GetEnumerator的公共实例定义。有什么想法,如何达到我的目的?

        foreach(Ellipse el in Halma){
           ellipsen.Add(el);
        }
    

    改写 以下代码将抛出错误:“对象引用未设置为对象的实例。”

     foreach (Ellipse el in Halma.Children){
         ellipsen.Add(el);
     }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   mm8    6 年前

    试试这个:

    public partial class MainWindow : Window
    {
        private readonly List<Ellipse> ellipsen = new List<Ellipse>();
        public MainWindow()
        {
            InitializeComponent();
            foreach (Ellipse el in Halma.Children.OfType<Ellipse>())
            {
                ellipsen.Add(el);
            }
        }
    }
    
        2
  •  0
  •   ManDani    6 年前

    foreach (Ellipse el in Halma.Children)
    {
        ellipsen.Add(el);
    }