3

我有画布 A 和画布 B。画布 A 包含一个图像和一个带有不同颜色方块的网格。 numSelectCan是不同的图像。

一旦我点击一个正方形,就会触发一个事件,该事件应该将可见性状态设置numSelectCan为可见,然后它应该显示与 Canvas A 重叠。

事实并非如此。我已经尝试了一切,但我根本无法numSelectCan出现。在开发numSelectCan过程中显示并且工作正常。在运行时 numSelectCan 刚刚消失。我尝试禁用 A 但仍然没有成功。

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="10,0,10,10">

        <Controls:RoundButton x:Name="btnCancel" PressedBrush="Orange" Foreground="White" BorderBrush="White" ImageSource="/Assets/Appgraphics/Buttons/cancel.png" HorizontalAlignment="Left" VerticalAlignment="Bottom" Click="btnCancel_Click"/>
        <Controls:RoundButton x:Name="btnQuestion" PressedBrush="Orange" Foreground="White" ImageSource="/Assets/Appgraphics/Buttons/questionmark.png" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,10,0,0" Click="btnQuestion_Click" BorderBrush="White"/>
        <Controls:RoundButton x:Name="btnConfirm" PressedBrush="Orange" Foreground="White" BorderBrush="White" ImageSource="/Assets/Appgraphics/Buttons/check.png" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="confirmSelection"/>
        <Canvas x:Name="picCanvas" HorizontalAlignment="Center" Width="582" Height="320">
            <Image x:Name="imgBackCC" Height="320" Width="582" Stretch="Fill" HorizontalAlignment="Center" Source="" Margin="0,0,0,0"/>
        </Canvas>

        <Canvas x:Name="numSelectCan" HorizontalAlignment="Center" Height="155" Visibility="Visible" Width="530" VerticalAlignment="Center">
            <Image x:Name="numSelBackground" Source="/Assets/Appgraphics/UserInterface/numFieldBackground.png" Height="155" Width="530" Stretch="Fill"/>

            <Image x:Name="numFieldButton1" Source="/Assets/Appgraphics/UserInterface/numField0.png" Width="70" Height="70" Canvas.Left="5" Canvas.Top="5"/>
            <Image x:Name="numFieldButton2" Source="/Assets/Appgraphics/UserInterface/numField1.png" Width="70" Height="70" Canvas.Left="80" Canvas.Top="5"/>
            <Image x:Name="numFieldButton3" Source="/Assets/Appgraphics/UserInterface/numField2.png" Width="70" Height="70" Canvas.Left="155" Canvas.Top="5"/>
            <Image x:Name="numFieldButton4" Source="/Assets/Appgraphics/UserInterface/numField3.png" Width="70" Height="70" Canvas.Left="230" Canvas.Top="5"/>
            <Image x:Name="numFieldButton5" Source="/Assets/Appgraphics/UserInterface/numField4.png" Width="70" Height="70" Canvas.Left="305" Canvas.Top="5"/>
            <Image x:Name="numFieldButton6" Source="/Assets/Appgraphics/UserInterface/numField5.png" Width="70" Height="70" Canvas.Top="80" Canvas.Left="5"/>
            <Image x:Name="numFieldButton7" Source="/Assets/Appgraphics/UserInterface/numField6.png" Width="70" Height="70" Canvas.Left="80" Canvas.Top="80"/>
            <Image x:Name="numFieldButton8" Source="/Assets/Appgraphics/UserInterface/numField7.png" Width="70" Height="70" Canvas.Left="155" Canvas.Top="80"/>
            <Image x:Name="numFieldButton9" Source="/Assets/Appgraphics/UserInterface/numField8.png" Width="70" Height="70" Canvas.Left="230" Canvas.Top="80"/>
            <Image x:Name="numFieldButton10" Source="/Assets/Appgraphics/UserInterface/numField9.png" Width="70" Height="70" Canvas.Left="305" Canvas.Top="80"/>
            <Image x:Name="numFieldDelButton" Source="/Assets/appgraphics/UserInterface/numFieldDelete.png" Width="145" Height="145" Canvas.Top="5" Canvas.Left="380" />
        </Canvas>

    </Grid>

</Grid>

不知道这里出了什么问题,或者如果我错过了一个简单的事情,比如为不同的画布设置显示顺序

任何想法/建议表示赞赏。

更新 我从我的 xaml 添加了整个内容面板。

这是页面初始化时为我创建网格的函数

public void createGrid()
    {
        //create the grid
        Grid backGrid = new Grid();
        backGrid.Width = 530;
        backGrid.Height = 285;
        backGrid.HorizontalAlignment = HorizontalAlignment.Center;
        backGrid.VerticalAlignment = VerticalAlignment.Center;
        backGrid.ShowGridLines = false;
        backGrid.Margin = new Thickness(25, 15, 0, 0);

        //define columns
        for (int c = 0; c < 10; c++)
        {
            ColumnDefinition colDef = new ColumnDefinition();
            backGrid.ColumnDefinitions.Add(colDef);
        }

        //define rows
        for (int r = 0; r < 6; r++)
        {
            RowDefinition rowDef = new RowDefinition();
            backGrid.RowDefinitions.Add(rowDef);
        }


        //colour counter
        int counter = 0;

        //create textboxes
        for (int r = 0; r < 6; r++)
        {
            for (int c = 0; c < 10; c++)
            {
                //set the coulour of the canvases based on the counter
                if (counter == 4)
                {
                    counter = 0;
                }

                SolidColorBrush tempBrush = new SolidColorBrush();

                switch (counter)
                {
                    case 0:
                        tempBrush = new SolidColorBrush(Colors.Red);
                        break;
                    case 1:
                        tempBrush = new SolidColorBrush(Colors.Orange);
                        break;
                    case 2:
                        tempBrush = new SolidColorBrush(Colors.Blue);
                        break;
                    case 3:
                        tempBrush = new SolidColorBrush(Colors.Green);
                        break;
                }

                string canName = c.ToString() + r.ToString();
                string txtName = "text" + c.ToString() + r.ToString();
                string tempCanName = "canvas" + c.ToString() + r.ToString();

                //creating the canvas
                Canvas tempCanvas = new Canvas();
                tempCanvas.Name = tempCanName;
                tempCanvas.Background = tempBrush;
                tempCanvas.HorizontalAlignment = HorizontalAlignment.Stretch;
                tempCanvas.VerticalAlignment = VerticalAlignment.Stretch;
                tempCanvas.Margin = new Thickness(2);


                //creating the textblock
                TextBlock tempName = new TextBlock();
                tempName.Width = 32;
                tempName.Height = 32;
                tempName.Name = txtName;
                tempName.Foreground = new SolidColorBrush(Colors.White);
                tempName.TextAlignment = TextAlignment.Center;
                tempName.Margin = new Thickness(13, 0, 0, 0);
                tempName.FontWeight = FontWeights.Bold;
                tempName.HorizontalAlignment = HorizontalAlignment.Center;
                tempName.VerticalAlignment = VerticalAlignment.Center;
                tempName.Visibility = Visibility.Visible;
                tempName.FontSize = 30;

                tempName.Tap += tempName_Tap;


                //adding the canvas to the grid
                Grid.SetRow(tempCanvas, r);
                Grid.SetColumn(tempCanvas, c);

                //adding all items into the canvas and into the grid
                tempCanvas.Children.Add(tempName);
                backGrid.Children.Add(tempCanvas);

                //increment counter
                counter++;
            }
        }

        //add the grid into the mainpage

        picCanvas.Children.Add(backGrid);

    }

所有这些都有效。我得到了所有不同颜色方块的网格,但是当我按下文本块时,我希望我的第二个画布弹出并且不会发生。我添加了断点,它通过了所有断点,我只是没有看到第二个画布

这是用于捕获文本块上的点击的代码

        //function that handels the event when a textblock is tapped
    private void tempName_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        //create a copy of the object that triggerd this event
        TextBlock tempBlock = sender as TextBlock;
        //create a string from the name of this textblock and then cut of the textpart from the name
        string tempName = tempBlock.Name;
        tempName = tempName.Substring(4);

        //move the canvas top or bottom based on which row the current selection is in
        string currentRow = tempName.Substring(1);
        if ((currentRow == "0") || (currentRow == "1") || (currentRow == "2"))
        {
            numSelectCan.VerticalAlignment = VerticalAlignment.Top;
        }
        else
        {
            numSelectCan.VerticalAlignment = VerticalAlignment.Bottom;
        }
        //show the number selector control
        numSelectCan.Visibility = Visibility.Visible;

    }

我仍然坚持这一点。来不及numSelectCan现身了...

4

2 回答 2

0

我拿走了你的 xaml/code 并用我自己的图像对其进行了测试。如果没有图像(或整个项目)来查看它们是如何在画布上绘制的,我不得不说您可能没有正确的 Z-Index 用于您的 2 个画布。它们通常层叠在另一个之上,而不是移到一侧。您可以通过设置SetZIndex. 尝试调用:

    //show the number selector control
    numSelectCan.Visibility = Visibility.Visible;
    Canvas.SetZIndex(numSelectCan, 5);

并再次隐藏它:

    //Hide the number selector control
    numSelectCan.Visibility = Visibility.Hidden;
    Canvas.SetZIndex(numSelectCan, -5);

当然,隐藏和更改 ZIndex 顺序可能有点多余。但是我在使用其中一张画布而不是显示的那张画布时遇到了麻烦,所以我两者都做。

于 2013-07-02T17:58:33.130 回答
0

你好,我已经尝试了你的代码,我发现你的问题有点令人困惑,所以我假设你希望你的第二个画布在点击文本块时可见。但是您没有在文本块上点击任何事件处理程序,因此没有触发事件,这就是为什么首先使您的文本块可见并添加一个点击事件处理程序,您可以在该处理程序上设置第二个画布的可见性。tempName_Tap eventHandler 是您为网格框事件处理程序着色而不是文本块点击事件处理程序,因此它永远不会注册您点击文本块。同样在开始时,您写道您希望您的第二个画布在网格框上点击时可见,因为您的代码工作正常。

于 2013-06-27T11:16:47.067 回答