Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF弹出位置=元素的中心和顶部_C#_Wpf_Xaml - Fatal编程技术网

C#WPF弹出位置=元素的中心和顶部

C#WPF弹出位置=元素的中心和顶部,c#,wpf,xaml,C#,Wpf,Xaml,im使用包含弹出控件的按钮。现在,我希望弹出控件在按钮顶部对齐,它也应该居中 <Style x:Key="ButtonStyle2" TargetType="{x:Type Button}"> <Setter Property="Foreground" Value="White" /> <Setter Property="Padding" Value="2" /> <Setter P

im使用包含弹出控件的按钮。现在,我希望弹出控件在按钮顶部对齐,它也应该居中

<Style x:Key="ButtonStyle2" TargetType="{x:Type Button}">
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Padding" Value="2" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Padding="{TemplateBinding Padding}" UseLayoutRounding="True">
                            <Grid>
                                <Popup x:Name="popup" Placement="Top" AllowsTransparency="True" PopupAnimation="Slide" HorizontalOffset="-7" IsOpen="False">
                                    <DockPanel Width="55" Background="#01FFFFFF">
                                        <Button x:Name="DeleteButton" DockPanel.Dock="Top" Margin="0,0,0,5" Style="{DynamicResource ButtonStyle3}" Background="Transparent" BorderThickness="0" Width="30" Height="30" Click="DeleteButton_Click" />
                                        <Button x:Name="EditButton" DockPanel.Dock="Top" Margin="0,0,0,5"  Style="{DynamicResource ButtonStyle4}" Background="Transparent" BorderThickness="0" Width="30" Height="30" Click="EditButton_Click" />
                                        <Button x:Name="AddButton" DockPanel.Dock="Bottom" Margin="0,0,0,5"  Style="{DynamicResource ButtonStyle5}" Background="Transparent" BorderThickness="0" Width="30" Height="30" Click="AddButton_Click" />
                                    </DockPanel>
                                </Popup>
                                <Image x:Name="add_img" Source="img\menu.png" RenderOptions.BitmapScalingMode="Fant" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="True">
                                    <Image.RenderTransform>
                                        <RotateTransform Angle="0" />
                                    </Image.RenderTransform>
                                </Image>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <EventTrigger RoutedEvent="Button.Click">
                                <BeginStoryboard Storyboard="{StaticResource HidePopup}" />
                            </EventTrigger>
                            <EventTrigger RoutedEvent="Button.MouseEnter">
                                <BeginStoryboard Storyboard="{StaticResource ShowPopup}" />
                                <BeginStoryboard>
                                    <Storyboard BeginTime="00:00:00.000" Duration="00:00:00.300">
                                        <DoubleAnimation Storyboard.TargetName="add_img" Storyboard.TargetProperty="RenderTransform.Angle" From="0" To="180" BeginTime="00:00:00.000" Duration="00:00:00.200" AutoReverse="False" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                            <EventTrigger RoutedEvent="Button.MouseLeave">
                                <BeginStoryboard Storyboard="{StaticResource HidePopup}" />
                                <BeginStoryboard>
                                    <Storyboard BeginTime="00:00:00.000" Duration="00:00:00.300">
                                        <DoubleAnimation Storyboard.TargetName="add_img" Storyboard.TargetProperty="RenderTransform.Angle" From="180" To="0" BeginTime="00:00:00.000" Duration="00:00:00.200" AutoReverse="False" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


代码片段可以工作,但如果我使用另一台计算机编译代码,弹出窗口将不再对齐。有更好的方法吗?

您忘记设置弹出窗口的PlacementTarget属性了吗

希望这有助于:

<Popup ... PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"></Popup>


谢谢您的回复。我将尝试你的代码片段,但我不确定这是否有帮助。好的。如果失败了,也许你应该给我们按钮控制模板的完整代码,以帮助我们测试我是否编辑了最上面的帖子。也许你现在可以看看。