UWP: Popup MaxWidth

2019-07-24 17:33发布

问题:

I have a Popup which contains an ItemsControl (I can use ListBox if needed) which its items are arranged horizontally.

Since the total width of the items can exceed the width of the screen, I need to limit the size of the popup width.

My question is how can I limit the size of the popup? I've tried to use MaxWidth, but it doesn't work :(

<Popup x:Name="puSoldItems" IsOpen="False" IsLightDismissEnabled="True" MaxWidth="{Binding ActualWidth, ElementName=_This}" ScrollViewer.HorizontalScrollMode="Auto" ScrollViewer.VerticalScrollMode="Auto">
    <Grid Background="#f8202020" MaxWidth="{Binding ActualWidth, ElementName=puSoldItems}">
        ...
        ...
        ...
        <ItemsControl x:Name="icItems" Grid.Row="1" Background="Transparent" Margin="10" MaxWidth="{Binding ActualWidth, ElementName=puSoldItems}" ScrollViewer.HorizontalScrollMode="Auto" ScrollViewer.VerticalScrollMode="Auto">
            ...
            ...
            ...
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Padding="0" Margin="0 0 0 10" BorderBrush="#afafaf" BorderThickness="0 0 0 1" MinWidth="350">
                        ...
                        ...
                        ...
                        <local:UPSoldItemList ItemsSource="{Binding Items}"></local:UPMenuModifier>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Popup>

Thanks...

回答1:

I partially solve this problem by forcing the width.

Here is what I did; I add ScrollViewer and put ItemsControl inside. Then when open the popup, I set the size of the ScrollViewer

private void btnItem_Tapped(object sender, TappedRoutedEventArgs e)
{
    svItems.MaxWidth = this.ActualWidth - 100;    //for padding (50 left & 50 right)
    svItems.MaxHeight = this.ActualHeight - 80;   //for padding (40 top & 40 bottom)
    puSoldItems.IsOpen = true;
}

Is anyone has a better solution, please kindly post it here.

Thanks!