为什么我的弹出窗口在某些机器上显示与放置属性相反?

10

我有一个简单的 WPF 弹出窗口,当用户点击按钮时我会显示它。

<Button
    x:Name="aButton"
    Content="Up/Down"
    Width="75"
    Height="30"
    Click="aButton_Click"
/>
<Popup
    PlacementTarget="{Binding ElementName=aButton}"
    Placement="Right"
    VerticalOffset="-31"
    StaysOpen="False"
    AllowsTransparency="True"
>
    <StackPanel>
        <Button Width="45" Height="45" Margin="2,0,2,2" Content="+"/>
        <Button Width="45" Height="45" Margin="2,0,2,0" Content="-"/>
    </StackPanel>
</Popup>

非常奇怪的是,这段代码在运行的机器不同上表现出不同的结果。我在我的台式电脑上运行它,一切运行得很好…也像它应该的那样。但是我在我的PDC09上运行它……弹出窗口的位置与我使用“Placement”属性告诉它的位置相反(在左侧而不是右侧)。

为什么会这样?我能做些什么来解决它?

2个回答

14

通过谷歌我找不到任何东西……但在WPF论坛中进行了幸运搜索,很快就找到了这个帖子提醒自己:如果谷歌找不到任何内容,请不要忘记搜索WPF论坛。

答案是我的PDC09 netbook本质上是一台平板电脑,显然,微软认为在配置为右撇子的平板电脑上显示弹出窗口与放置属性相反是一个好主意…… 这样弹出窗口就不会出现在用户的手下面。

解决方案是恢复自定义Popup放置方式……如果您不想要这种行为。

我很乐意听听有关解决此问题的其他方法。


顺便说一句,我总是在 Stack Overflow 上搜索,以找到其他人在适当的论坛上发现的内容。 ;) - cunningdave
1
很高兴能帮助你……这种事情总是让我抓狂。 - cplotts

1
我通过在与所需放置目标相同的网格列/行中添加边框来解决了这个问题。 然后将其设置为放置目标。 通过将此边框的宽度绑定到弹出内容,它将自动调整其宽度,因此对齐(左侧或右侧)无关紧要。 如果您仍然想控制对齐方式,可以通过对齐放置目标边框来实现。 希望这有意义,如果没有,这是一个快速示例。
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Popup x:Name="StartMenuPopup" Placement="Top" PlacementTarget="{Binding ElementName=PopupTarget}" >
        <Border x:Name="PopupBorder">
        </Border>
    </Popup>

    <Border x:Name="PopupTarget" Grid.Row="1" Width="{Binding ActualWidth, Mode=OneWay, ElementName=PopupBorder}" 
        BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Top"/>

    <startmenu:TaskBar Grid.Row="1">
        <startmenu:TaskBar.StartButton>
            <startmenu:ToggleMenu Width="36" x:Name="StartButton"
                              ImageData="{Binding StartButtonImageData}"
                              AssociatedPopup="{Binding ElementName=StartMenuPopup}"
                              IsOpen="{Binding StartMenuOpen, Mode=TwoWay}"/>
        </startmenu:TaskBar.StartButton>
    </startmenu:TaskBar>
</Grid>

弹出窗口的PlacementTarget绑定到PopupTarget边框,而PopupTarget边框宽度则绑定回PopupBorder元素。这使得PopupTarget边框与弹出窗口的宽度相同,从而消除了对齐问题。

我知道这有点过时,但是我想说这个方法在我测试过的两种对齐类型的计算机上都有效。我给border添加了一个边距以使其正确对齐。 - Thomas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接