首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Silverlight按钮事件未触发

Silverlight按钮事件未触发
EN

Stack Overflow用户
提问于 2012-09-24 18:22:50
回答 1查看 971关注 0票数 1

我知道以前有人问过这样的问题,但我已经尝试过这些建议,但都无济于事,所以希望一些新的观点能在这方面帮助我。

我有一个自定义控件,它基本上是一个带网格的边框,里面有两个文本框和一个按钮。

该控件还有一个八角形,它被添加到代码隐藏中。

该按钮的click事件不会激发。我猜这可能与它上面的控件获取click事件而不是Button有关,但我不知道如何解决它。或者可能是因为八角形是后来在代码后面绘制的。

我尝试了很多不同的方法,移动按钮,添加另一个网格,将背景设置为透明。这快把我逼疯了。

下面是代码。

XAML

代码语言:javascript
复制
<UserControl x:Class="HSCGym.UserControls.CustomErrorControl"
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"            
mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="1000">
    <Border x:Name="ErrorBorder" BorderBrush="Black" BorderThickness="2" CornerRadius="20" Background="White"
            Width="900" Height="700"
            VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
            Grid.Row="0" Grid.RowSpan="2">
        <Border.Effect>
            <DropShadowEffect BlurRadius="7" Direction="300" ShadowDepth="6" Color="Black" />
        </Border.Effect>

        <Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent" >
            <Grid.RowDefinitions>
            <RowDefinition Height="55" />
            <RowDefinition Height="55" />
            <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <TextBlock Text="{Binding ErrorText}" HorizontalAlignment="Center" FontSize="40" Grid.Row="0"/>
            <TextBlock Text="{Binding ErrorText2}" HorizontalAlignment="Center" FontSize="40" Grid.Row="1"/>

        <Button x:Name="btnExit" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="38,0,0,20"
                    Content="Exit" Height="50" Width="200"  FontSize="20" Click="btnExit_Click"
                Grid.Row="2"/>
        </Grid>
    </Border>
</UserControl>

和背后的代码:

代码语言:javascript
复制
public partial class CustomErrorControl
{
    public string ErrorText { get; set; }

    public string ErrorText2 { get; set; }

    public string StopText { get; set; }

    private readonly int x;
    private readonly int y;
    private readonly int r;

    public CustomErrorControl(int x, int y, int radius, string stopError)
    {
        InitializeComponent();

        this.x = x;
        this.y = y;
        r = radius;

        StopText = stopError;

        DrawOctagon(this.x, this.y, r);


    }
    public void DrawOctagon(int x, int y, int R)
    {
        int r2 = (int)(R/Math.Sqrt(2));

        // OuterOctagon
        Point[] outerOctagon = new Point[8];
        outerOctagon[0].X = x;
        outerOctagon[0].Y = y - R;

        outerOctagon[1].X = x + r2;
        outerOctagon[1].Y = y - r2;

        outerOctagon[2].X = x + R;
        outerOctagon[2].Y = y;

        outerOctagon[3].X = x + r2;
        outerOctagon[3].Y = y + r2;

        outerOctagon[4].X = x;
        outerOctagon[4].Y = y + R;

        outerOctagon[5].X = x - r2;
        outerOctagon[5].Y = y + r2;

        outerOctagon[6].X = x - R;
        outerOctagon[6].Y = y;

        outerOctagon[7].X = x - r2;
        outerOctagon[7].Y = y - r2;

        HexColor stop1Colour = new HexColor("#FFA30D0D");
        HexColor stop2Colour = new HexColor("#FFCA0C0C");
        HexColor stop3Colour = new HexColor("#FFF71212");

        GradientStop gs1 = new GradientStop { Color = stop1Colour, Offset = 0.98 };
        GradientStop gs2 = new GradientStop { Color = stop2Colour, Offset = 0.5 };
        GradientStop gs3 = new GradientStop { Color = stop3Colour, Offset = 0.04 };

        LinearGradientBrush gb = new LinearGradientBrush
                                     {
                                         StartPoint = new Point(0.77, 0.85),
                                         EndPoint = new Point(0.13, 0.15),
                                         GradientStops = new GradientStopCollection {gs1, gs2, gs3}
                                     };

        DropShadowEffect dse = new DropShadowEffect
                                   {
                                       BlurRadius = 8,
                                       Color = Colors.Black,
                                       Direction = 320,
                                       ShadowDepth = 10.0,
                                       Opacity = 0.5
                                   };

        Polygon octagonOuter = new Polygon
        {

            VerticalAlignment = VerticalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Center,
            Fill = gb,

            Stroke = new SolidColorBrush(Colors.Black),
            StrokeThickness = 5,
            StrokeDashArray = new DoubleCollection { 10, 0 },
            Effect = dse,
            Points = new PointCollection
                        {
                            new Point(outerOctagon[0].X, outerOctagon[0].Y),
                            new Point(outerOctagon[1].X, outerOctagon[1].Y),
                            new Point(outerOctagon[2].X, outerOctagon[2].Y),
                            new Point(outerOctagon[3].X, outerOctagon[3].Y),
                            new Point(outerOctagon[4].X, outerOctagon[4].Y),
                            new Point(outerOctagon[5].X, outerOctagon[5].Y),
                            new Point(outerOctagon[6].X, outerOctagon[6].Y),
                            new Point(outerOctagon[7].X, outerOctagon[7].Y),
                        }
        };

        double outerOctCenterY = octagonOuter.Points[6].Y - octagonOuter.Points[2].Y;
        double outerOctCenterX = octagonOuter.Points[4].X - octagonOuter.Points[0].X;

        var rotate = new RotateTransform { Angle = 22.8, CenterX = outerOctCenterX, CenterY = outerOctCenterY };
        octagonOuter.RenderTransform = rotate;
        Grid.SetRow(octagonOuter, 2);

        TextBlock tbStopError = new TextBlock
                                    {
                                        Text = StopText,
                                        Foreground = new SolidColorBrush(Colors.White),
                                        FontFamily = new FontFamily("Courier New"),
                                        FontSize = 80,
                                        FontWeight = FontWeights.Bold,
                                        VerticalAlignment = VerticalAlignment.Center,
                                        HorizontalAlignment = HorizontalAlignment.Center
                                    };
        Grid.SetRow(tbStopError, 2);

        LayoutRoot.Children.Add(octagonOuter);
        LayoutRoot.Children.Add(tbStopError);   
    }

    private void btnExit_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Clicked");
    }

}

有什么想法吗?

非常感谢

尼尔

更新:

我已经测试了如何快速地将MouseLeftButtonUp事件放在网格和边界上。

当我单击边框时,首先触发网格事件,然后触发边框事件,但如果单击该按钮,则不会触发任何内容。这是否意味着Button位于网格和边框之上,因此应该触发事件?

我也会在适当的时候尝试一些其他的建议。

尼尔

更新2:

它变得越来越奇怪。我将按钮移到顶部,然后单击event fires。我所看到的是,从某一Y点及以下,绝对没有鼠标左键向上或单击事件触发,没有。这没有任何意义。如果我从底部开始并单击moving up,那么在某个点上,所有事件都会开始触发。

尼尔

更新3

大家好,

在逐个删除控件以查看导致问题的原因后,我现在知道它是边界控件。如果我移除它,一切正常,当它回来的时候,和以前一样的问题。有什么想法吗?

谢谢

尼尔

更新4

大家好,

好了,我终于解决了这个问题,以防有人发现自己遇到了类似的情况。这和那个页面一点关系都没有。我正在从一个不同的页面加载一个框架中的页面,它似乎是我必须在主页上的网格中设置许多行定义。在我纠正了这一点之后,一切都很好。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2012-09-24 19:18:45

首先尝试按透明颜色填充按钮(00000000)。

您也可以发送解决方案或它的一部分给我,我将努力帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12563067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档