开发者

Why is my Popup showing opposite the Placement property on some machines?

I have a simple WPF Popup that I am showing when the user clicks a Button.

<Button
    x:Name="aButton"
    Content="Up/Down"
    Width="75"
    Height="30"
    Click="aButton_Click"
/>
<Popup
    PlacementTarget="{Binding ElementName=aButton}"
    Placement="Right"
    VerticalOf开发者_开发知识库fset="-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>

What is extremely weird ... is that this code works differently depending on what machine it runs on.

I run this code on my main desktop and everything works just fine ... and as it should. I run it on my PDC09 netbook ... and the Popup shows opposite (on the left instead of the right as I told it to with the Placement property).

Why is this? And what can I do about it?


I couldn't find anything via Google ... but a lucky search in the WPF forum, quickly found this post. Note to self: don't forget to search the WPF forums if Google can't find anything.

The answer is that my PDC09 netbook is a Tablet PC at heart, and apparently, Microsoft thought it was a good idea to show the Popup opposite to the Placement property on a Tablet PC that is configured for right-handed people ... such that the Popup doesn't appear under the user's hand.

The solution is to revert to custom Popup placement ... if you don't want this behavior.

I would love to hear about any other ways around this problem.


I fixed this issue by adding a border in the same grid col/row as the desired placement target. Then set this as the placement target instead. By binding the width of this border to the popup content it will adjust it's width automatically therefore the alignment (left or right) is irrelevant. If you want to still control alignment, you can do that by aligning the placement target border. Hope that makes sense, if not, here is a quick example.

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

The popup PlacementTarget binds to the PopupTarget border, and the PopupTarget border width binds back to the PopupBorder element. This makes the PopupTarget border the same width as the popup therefore negating the alignment issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜