1

我知道以前有人问过这类问题,但我尝试了这些建议无济于事,所以希望一些新鲜的眼睛可以帮助我解决这个问题。

我有一个自定义控件,它基本上是一个带有网格的边框,其中包含 2 个文本框和一个按钮。

该控件还有一个在代码隐藏中添加的八边形。

按钮的单击事件不会触发。我收集它可能与它上面的控件获取点击事件而不是按钮有关,但我不知道如何解决它。或者可能是因为八边形是后来在后面的代码中绘制的。

我尝试了很多不同的东西,移动按钮,添加另一个网格,将背景设置为透明。这让我发疯了。

下面是代码。

XAML

<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>

以及背后的代码:

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:

它变得陌生。我将按钮移到顶部,然后单击事件触发。我看到的是,从某个 Y 点及以下,绝对没有 mouseleftbuttonup 或 click 事件触发,nada。这是没有意义的。如果我从底部开始并单击缓慢向上移动,那么在某个点,所有事件都开始触发。

尼尔

更新 3

大家好,

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

谢谢

尼尔

更新 4

大家好,

好的,所以我终于解决了问题,以防万一有人发现自己处于类似情况。它与那个页面完全没有关系。我正在从不同页面的框架中加载页面,看起来我必须在主页的网格中设置许多行定义。在我更正之后,一切正常。

谢谢

4

1 回答 1

0

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

您也可以将解决方案或其中的一部分发送给我,我会尽力提供帮助。

于 2012-09-24T11:18:45.030 回答