如何获得不同位置的数字

时间:2018-10-09 16:31:41

标签: c# wpf

当我单击按钮时,我只想得到一个数字和一个圆圈,因此,如果我单击按钮3次,我显然会得到带有圆圈的3个数字。假定每次单击后都会出现彩票号码,但我不知道该怎么办。

    private void btnGo_Click(object sender, RoutedEventArgs e)
    {
        Ellipse first = new Ellipse();
        first.Fill = new SolidColorBrush(Colors.Red);
        first.Height = 70;
        first.Width = 70;
        first.Margin = new Thickness(50, 100, 0, 0);
        caPaper.Children.Add(first);

        Ellipse second = new Ellipse();
        second.Fill = new SolidColorBrush(Colors.Red);
        second.Height = 70;
        second.Width = 70;
        second.Margin = new Thickness(150, 100, 0, 0);
        caPaper.Children.Add(second);

        Ellipse third = new Ellipse();
        third.Fill = new SolidColorBrush(Colors.Red);
        third.Height = 70;
        third.Width = 70;
        third.Margin = new Thickness(250, 100, 0, 0);
        caPaper.Children.Add(third);

        Random rd = new Random();

        TextBlock txt1 = new TextBlock();
        txt1.FontSize = 20;
        txt1.Foreground = Brushes.White;
        txt1.Text = " " + rd.Next(1, 45);
        Canvas.SetTop(txt1, 120);
        Canvas.SetLeft(txt1, 70);
        caPaper.Children.Add(txt1);

        TextBlock txt2 = new TextBlock();
        txt2.FontSize = 20;
        txt2.Foreground = Brushes.White;
        txt2.Text = " " + rd.Next(1, 45);
        Canvas.SetTop(txt2, 120);
        Canvas.SetLeft(txt2, 170);
        caPaper.Children.Add(txt2);

        TextBlock txt3 = new TextBlock();
        txt3.FontSize = 20;
        txt3.Foreground = Brushes.White;
        txt3.Text = " " + rd.Next(1, 45);
        Canvas.SetTop(txt3, 120);
        Canvas.SetLeft(txt3, 270);
        caPaper.Children.Add(txt3);

    }

    private void btnClear_Click(object sender, RoutedEventArgs e)
    {
        caPaper.Children.Clear();

2 个答案:

答案 0 :(得分:3)

这是错误方法的100%。

您应该从不动态创建这样的UI控件。太错误了,不值得弄清楚代码中的错误。

使用MVVM,它将使您的生活大大简化。您的彩票号码是数据,球是该数据的直观表示。因此,在您的VM中:

public ObservableCollection<int> Draws {get;} = new ObservableCollection<int>();

// Invoked from command, or could be a click handler until you learn commands
public AddDraw()
{
     int draw = GetNextDraw(); //Left as an exercise
     Draws.Add(draw)
}

现在已经处理了数据,我们需要显示它。 ItemsControl是显示集合的方式,使用ItemTemplate控制数字的显示方式(球),使用ItemsPanel控制布局(水平的WrapPanel您想要什么):

<ItemsControl ItemsSource="{Binding Draws}">
   <ItemsControl.ItemTemplate>
       <DataTemplate>
          <Grid>
             <Ellipse Fill="Red/>
             <TextBlock Text="{Binding Path=.}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
          </Grid>
       </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
       <ItemPanelTemplate>
          <WrapPanel Orientation="Horizontal"/>
       </ItemPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

那是我的头上的东西,所以我可能有一个错误的属性或类。 Intellisense应该很快纠正它。

一旦设置好,您就可以添加边距,更改颜色。 WPF就是为此设计而设计的,请充分利用它。

免责声明:我不得不处理所有ONCE上创建UI控件的问题。在7年的WPF专业工作中,仅仅是因为表示的数据结构非常复杂(并且与表示方式不同)。

答案 1 :(得分:0)

As mentioned below,您的方法可能需要一些调整。 WPF具有一些非常强大的数据绑定功能,可以更轻松地构建UI。但是要回答你的问题...

每个球唯一看起来唯一的是球的边距和Canvas.SetLeft中的左值

您应该能够基于画布中子代的数量得出这些值。例如这样的东西:

int marginLeft = 50 + (caPaper.Children.Length * 100);

Ellipse newBall = new Ellipse();
newBall.Fill = new SolidColorBrush(Colors.Red);
newBall.Height = 70;
newBall.Width = 70;
newBall.Margin = new Thickness(marginLeft, 100, 0, 0);
caPaper.Children.Add(newBall);

您当然需要进行调整,但是我想给您一个思路。同样,将需要对文本进行相同的操作。然后,每次单击它,它都会根据已经拥有的孩子数创建它们。

相关问题